次の方法で共有


既定の NetTcpBinding

既定のサンプルでは、NetTcpBinding バインドの使用方法を示します。 このサンプルは、電卓サービスを実装する 作業の開始 に基づいています。 このサンプルでは、サービスはセルフホステッドです。 クライアントとサービスはどちらもコンソール アプリケーションです。

このサンプルのセットアップ手順とビルド手順は、このトピックの最後にあります。

バインディングは、クライアントとサービスの構成ファイルで指定されます。 バインドの種類は、次のサンプル構成に示すように、<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 サンプル One-Time セットアップ手順を実行していることを確認します。

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

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

    サーバーはセルフホステッドであるため、クロスマシン構成でサンプルを実行するには、クライアントの 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>