複数のエンドポイント

この複数のエンドポイントのサンプルでは、複数のエンドポイントを 1 つのサービスに構成する方法と、クライアントから各エンドポイントと通信を行う方法を示します。 このサンプルは、「入門サンプル」に基づいています。 サービス構成は、ICalculator コントラクトをサポートする 2 つのエンドポイントを定義するように変更されていますが、各エンドポイントは異なるアドレスで異なるバインディングを使用します。 クライアント構成とコードは、両方のサービス エンドポイントと通信するように変更されています。

Note

このサンプルのセットアップ手順とビルド手順については、このトピックの最後を参照してください。

サービスの Web.config ファイルは、2 つのエンドポイントを定義するように変更されています。各エンドポイントは同じ ICalculator コントラクトをサポートしますが、異なるアドレスで異なるバインディングを使用します。 1 つ目のエンドポイントは、basicHttpBinding バインディングを使用して基本アドレスで定義されます。このエンドポイントではセキュリティは有効ではありません。 2 つ目のエンドポイントは、wsHttpBinding バインディングを使用して {baseaddress}/secure で定義されます。このエンドポイントは既定で、Windows 認証による WS-Security を使用してセキュリティ保護されています。

<service
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- This endpoint is exposed at the base address provided by host:
       http://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- secure endpoint exposed at {base address}/secure:
       http://localhost/servicemodelsamples/service.svc/secure -->
  <endpoint address="secure"
            binding="wsHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  ...
</service>

両方のエンドポイントは、さらにクライアントでも構成されます。 呼び出し元が必要なエンドポイント名をクライアントのコンストラクタに渡せるように、これらのエンドポイントには名前が付けられます。

<client>
  <!-- Passing "basic" into the constructor of the CalculatorClient
       class selects this endpoint.-->
  <endpoint name="basic"
            address="http://localhost/servicemodelsamples/service.svc"
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- Passing "secure" into the constructor of the CalculatorClient
       class selects this endpoint.-->
  <endpoint name="secure"
            address="http://localhost/servicemodelsamples/service.svc/secure"
            binding="wsHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
</client>

次のコードに示すように、クライアントは両方のエンドポイントを使用します。

static void Main()
{
    // Create a client to the basic endpoint configuration.
    CalculatorClient client = new CalculatorClient("basic");
    Console.WriteLine("Communicate with basic endpoint.");
    // call operations
    DoCalculations(client);

    // Close the client and release resources.
    client.Close();

    // Create a client to the secure endpoint configuration.
    client = new CalculatorClient("secure");
    Console.WriteLine("Communicate with secure endpoint.");
    // Call operations.
    DoCalculations(client);

    // Close the client and release resources.
    client.Close();

    Console.WriteLine();
    Console.WriteLine("Press <ENTER> to terminate client.");
    Console.ReadLine();
}

クライアントを実行すると、両方のエンドポイントとのやり取りが表示されます。

Communicate with basic endpoint.
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Communicate with secure endpoint.
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

サンプルをセットアップ、ビルド、および実行するには

  1. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順を実行したことを確認します。

  2. ソリューションの C# 版または Visual Basic .NET 版をビルドするには、「 Building the Windows Communication Foundation Samples」の手順に従います。

  3. 単一または複数コンピューター構成でサンプルを実行するには、「Windows Communication Foundation サンプルの実行」の手順に従います。