Share via


方法 : 基本的な Windows Communication Foundation クライアントを構成する

これは、基本的な Windows Communication Foundation (WCF) サービスとそのサービスを呼び出すことができるクライアントの作成に必要な 6 つのタスクのうち、5 番目のタスクです。6 つのすべてのタスクの概要については、「チュートリアル入門」を参照してください。

ここでは、Service Model Metadata Utility (Svcutil.exe) を使用して生成されたクライアント構成ファイルをクライアント プロジェクトに追加し、クライアント構成要素の内容について詳しく説明します。クライアントを構成するには、クライアントがサービスにアクセスするために使用するエンドポイントを指定します。エンドポイントには、アドレス、バインディング、およびコントラクトがあり、クライアントを構成する過程でそれぞれを指定する必要があります。

クライアント用に生成された構成ファイルの内容については、手順の後の例に示します。

Windows Communication Foundation クライアントを構成するには

  1. Visual Studio で、前の「方法 : Windows Communication Foundation クライアントを作成する」の手順で生成された App.config 構成ファイルをクライアント プロジェクトに追加します。ソリューション エクスプローラで、クライアント プロジェクトを右クリックし、[追加] をポイントして、[既存の項目] をクリックします。次に、C:\Users\<user name>\Documents\Visual Studio 2005\Projects\Service\Client ディレクトリから App.config 構成ファイルを選択します (構成ファイルに App.config という名前が付けられているのは、Svcutil.exe ツールでこのファイルを生成したときに、/config:app.config スイッチを使用したためです)。[OK] をクリックします。既定の [既存項目の追加] ダイアログ ボックスでは、.config 拡張子のファイルはすべてフィルタで除外されます。これらのファイルを表示するには、[既存項目の追加] ダイアログ ボックスの右下にあるドロップダウン リスト ボックスで [すべてのファイル (*.*)] を選択します。

  2. 生成された構成ファイルを開きます。Svcutil.exe によって、バインディングの設定ごとに値が生成されます。生成された構成ファイルを次の例に示します。<system.serviceModel> セクション内に <endpoint> 要素があるのがわかります。次の構成ファイルは、生成されたファイルの簡略化バージョンです。

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <wsHttpBinding>
            <binding name="WSHttpBinding_ICalculator">
            </binding>
          </wsHttpBinding>
        </bindings>
        <client>
          <endpoint
               address="https://localhost:8000/ServiceModelSamples/Service/CalculatorService"
               binding="wsHttpBinding"
               bindingConfiguration="WSHttpBinding_ICalculator"
               contract="Microsoft.ServiceModel.Samples.ICalculator"
               name="WSHttpBinding_ICalculator">
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration> 
    

    この例では、アドレス https://localhost:8000/ServiceModelSamples/service に配置されたサービスにアクセスするために、クライアントが使用するエンドポイントを構成しています。

    エンドポイント要素では、システム指定の WsHttpBinding で構成された通信に Microsoft.ServiceModel.Samples.ICalculator コントラクトを使用することを指定します。このバインディングは、トランスポートとして HTTP を指定し、相互運用可能なセキュリティ、およびその他の構成詳細を指定します。

  3. この構成で生成されたクライアントを使用する方法詳細については、 、「方法 : Windows Communication Foundation クライアントを使用する」を参照してください。

この例では、クライアント用に生成された構成ファイルの内容を示します。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator"     
          closeTimeout="00:01:00"
          openTimeout="00:01:00" 
          receiveTimeout="00:10:00" 
          sendTimeout="00:01:00"
          bypassProxyOnLocal="false" 
          transactionFlow="false"  
          hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" 
          maxReceivedMessageSize="65536"
          messageEncoding="Text" 
          textEncoding="utf-8" 
          useDefaultWebProxy="true"
          allowCookies="false">
          <readerQuotas maxDepth="32" 
            maxStringContentLength="8192" 
            maxArrayLength="16384"
            maxBytesPerRead="4096" 
            maxNameTableCharCount="16384" />
          <reliableSession ordered="true" 
            inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" 
              proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" 
              negotiateServiceCredential="true"
              algorithmSuite="Default" 
              establishSecurityContext="true" />
           </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <client>
    <endpoint address="https://localhost:8000/ServiceModelSamples/Service/CalculatorService"
      binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_ICalculator"
      contract="ICalculator" 
      name="WSHttpBinding_ICalculator">
        <identity>
          <userPrincipalName value="user@contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

これでクライアントが構成されました。「方法 : Windows Communication Foundation クライアントを使用する」に進んでください。トラブルシューティングの詳細については、「チュートリアル入門のトラブルシューティング」を参照してください。

関連項目

タスク

方法 : Windows Communication Foundation クライアントを作成する

概念

サービスとクライアントを構成するためのバインディングの使用

その他の技術情報

Service Metadata Utility (Svcutil.exe)
Getting Started Sample
Self-Host