方法: コード内にサービス エンドポイントを作成する

この例では、電卓サービスに 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) または他のオーバーロードの 1 つを呼び出して、ホストのサービス エンドポイントを追加します。

    
    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 サービスの簡易構成」を参照してください。

関連項目