次の方法で共有


既定の NetTcpBinding

このサンプルは、NetTcpBinding バインディングの使用方法を示します。このサンプルは、電卓サービスを実装する「入門サンプル」に基づいています。このサンプルでは、サービスは自己ホスト型です。クライアントとサービスは両方ともコンソール アプリケーションです。

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

ms752250.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\Binding\Net\TCP\Default

バインディングは、クライアントとサービスの構成ファイルに指定されます。バインディングの種類は、<endpoint> 要素の binding 属性で指定します。次のサンプル構成を参照してください。

<endpoint address=""
          binding="netTcpBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />

前のサンプルでは、netTcpBinding バインディングを既定の設定で使用するようエンドポイントを構成する手順を示しました。netTcpBinding バインディングを構成してその設定の一部を変更する場合は、バインディング構成を定義する必要があります。エンドポイントは、バインディング構成を bindingConfiguration 属性を使用して名前で参照します。このサンプルでは、バインディング構成の名前は Binding1 です。これは次のサンプル構成のように定義されます。

<services>
  <service name="Microsoft.ServiceModel.Samples.CalculatorService"
           behaviorConfiguration="CalculatorServiceBehavior">
    ...
    <endpoint address=""
              binding="netTcpBinding"
              bindingConfiguration="Binding1" 
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    ...
  </service>
</services>

<bindings>
  <netTcpBinding>
    <binding name="Binding1" 
             closeTimeout="00:01:00"
             openTimeout="00:01:00" 
             receiveTimeout="00:10:00" 
             sendTimeout="00:01:00"
             transactionFlow="false" 
             transferMode="Buffered" 
             transactionProtocol="OleTransactions"
             hostNameComparisonMode="StrongWildcard" 
             listenBacklog="10"
             maxBufferPoolSize="524288" 
             maxBufferSize="65536" 
             maxConnections="10"
             maxReceivedMessageSize="65536">
      <readerQuotas maxDepth="32" 
                    maxStringContentLength="8192" 
                    maxArrayLength="16384"
                    maxBytesPerRead="4096" 
                    maxNameTableCharCount="16384" />
      <reliableSession ordered="true" 
                       inactivityTimeout="00:10:00"
                       enabled="false" />
      <security mode="Transport">
        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
      </security>
    </binding>
  </netTcpBinding>
</bindings>

このサンプルを実行すると、操作要求および応答がクライアントのコンソール ウィンドウに表示されます。クライアントをシャットダウンするには、クライアント ウィンドウで Enter キーを押します。

    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. 次のコマンドを使用して、ASP.NET 4.0 をインストールします。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順」が実行済みであることを確認します。

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

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

    ms752250.note(ja-jp,VS.100).gif注 :
    サーバーは自己ホスト型なので、このサンプルを複数コンピューター構成で実行する場合は、次のようにクライアントの App.config ファイルで ID を指定する必要があります。

    <client>
      <endpoint name=""
          address="net.tcp://servername:9000/servicemodelsamples/service" 
          binding="netTcpBinding" 
          contract="Microsoft.ServiceModel.Samples.ICalculator">
            <identity>
              <userPrincipalName value = "user_name@service_domain"/>
            </identity>
      </endpoint>
    </client>