作法:在程式碼中建立服務端點

在此範例中,已為計算機服務定義了 ICalculator 合約、在 CalculatorService 類別中實作了服務,並於程式碼中定義其端點,同時指定服務必須使用 BasicHttpBinding 類別。

通常最佳作法是在組態中以宣告方式指定繫結和位址資訊,而不是在程式碼中強制指定。 在程式碼中定義端點通常不太實用,因為部署之服務的繫結和位址通常與開發服務時所使用的繫結和位址不同。 比較一般性的作法是將繫結和位址資訊留在程式碼外面,如此一來,不需要重新編譯或重新部署應用程式,就可以變更繫結和位址資訊。

若要在程式碼中建立服務端點

  1. 建立可定義服務合約的介面。

    [ServiceContract]
    public interface ICalculator
    {
       [OperationContract]
       double Add(double n1, double n2);
       [OperationContract]
       double Subtract(double n1, double n2);
       [OperationContract]
       double Multiply(double n1, double n2);
       [OperationContract]
       double Divide(double n1, double n2);
    }
    
    
    <ServiceContract()> _
    Public Interface ICalculator
    
        <OperationContract()> _
        Function Add(ByVal n1 As Double, _
                     ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, ByVal _
                          n2 As Double) As Double
    
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, _
                        ByVal n2 As Double) As Double
    
    End Interface
    
    
  2. 實作步驟 1 中定義的服務合約。

    public class CalculatorService : ICalculator
    {
       public double Add(double n1, double n2)
       {
          return n1 + n2;
       }
       public double Subtract(double n1, double n2)
       {
          return n1 - n2;
       }
       public double Multiply(double n1, double n2)
       {
          return n1 * n2;
       }
       public double Divide(double n1, double n2)
       {
          return n1 / n2;
       }
    }
    
    
    Public Class CalculatorService
        Implements ICalculator
    
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function
    
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function
    
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function
    
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    
    End Class
    
    
  3. 在裝載應用程式中,建立服務的基底位址以及要與服務一起搭配使用的繫結。

    
    // Specify a base address for the service
    
    String baseAddress = "http://localhost/CalculatorService";
    // Create the binding to be used by the service.
    
    BasicHttpBinding binding1 = new BasicHttpBinding();
    
    
    ' Specify a base address for the service
    Dim baseAddress = "http://localhost/CalculatorService"
    ' Create the binding to be used by the service.
    
    Dim binding1 As New BasicHttpBinding()
    
  4. 建立主機並呼叫 ServiceHost.AddServiceEndpoint(Type, Binding, String) 或其他多載中的一個,為主機新增服務端點。

    
    using(ServiceHost host = new ServiceHost(typeof(CalculatorService)))
    {
        host.AddServiceEndpoint(typeof(ICalculator),binding1, baseAddress);
    
    
    
    Using host As New ServiceHost(GetType(CalculatorService))
        With host
            .AddServiceEndpoint(GetType(ICalculator), _
                                    binding1, _
                                    baseAddress)
    
    

    若要在程式碼中指定繫結,但不使用執行階段提供的預設端點,請於建立 ServiceHost 時將基底位址傳遞至建構函式,且不要呼叫 ServiceHost.AddServiceEndpoint

    ServiceHost host = new ServiceHost(typeof(CalculatorService), new Uri(baseAddress));
    
    Dim host As New ServiceHost(GetType(CalculatorService), New Uri(baseAddress))
    

    如需預設端點的詳細資訊,請參閱簡化的組態WCF 服務的簡化組態

另請參閱