共用方式為


作法:在程式碼中指定服務繫結

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

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

如需了解如何使用組態元素而非程式碼來設定此服務,請參閱操作說明:在組態中指定服務繫結

若要在程式碼中指定使用服務的 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. 在服務類別中實作服務合約。

    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. 建立服務的主機、新增端點,然後開啟主機。

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

若要修改繫結屬性的預設值

  1. 若要修改 BasicHttpBinding 類別的一個預設屬性值,請在建立主機之前,先將繫結上的屬性值設為新值。 例如,若要將預設的開啟與關閉逾時值由 1 分鐘改為 2 分鐘,請使用下列值。

    
    TimeSpan modifiedCloseTimeout = new TimeSpan(00, 02, 00);
    binding1.CloseTimeout = modifiedCloseTimeout;
    
    
    Dim modifiedCloseTimeout As New TimeSpan(0, 2, 0)
    binding1.CloseTimeout = modifiedCloseTimeout
    

另請參閱