次の方法で共有


複数のエンドポイント

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

ms751515.note(ja-jp,VS.100).gif注 :
このサンプルのセットアップ手順とビルド手順については、このトピックの最後を参照してください。

サービスの 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:       https://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- secure endpoint exposed at {base address}/secure:       https://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="https://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="https://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 版をビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。

  3. 単一コンピューター構成か複数コンピューター構成かに応じて、「Running the Windows Communication Foundation Samples」の手順に従います。

ms751515.Important(ja-jp,VS.100).gif 注 :
サンプルは、既にコンピューターにインストールされている場合があります。続行する前に、次の (既定の) ディレクトリを確認してください。

<InstallDrive>:\WF_WCF_Samples

このディレクトリが存在しない場合は、「.NET Framework 4 向けの Windows Communication Foundation (WCF) および Windows Workflow Foundation (WF) のサンプル」にアクセスして、Windows Communication Foundation (WCF) および WF のサンプルをすべてダウンロードしてください。このサンプルは、次のディレクトリに格納されます。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Services\MultipleEndpoints