WCF サービス パフォーマンスを制御するための ServiceThrottlingBehavior の使用

ServiceThrottlingBehavior クラスでは、アプリケーション レベルで作成されるインスタンス数またはセッション数の制限に使用できるプロパティが公開されています。 この動作を使用すると、Windows Communication Foundation (WCF) アプリケーションのパフォーマンスを微調整できます。

サービス インスタンスと同時呼び出しの制御

MaxConcurrentCalls プロパティを使用して、ServiceHost クラスでアクティブに処理されるメッセージの最大数を指定します。また、MaxConcurrentInstances プロパティを使用して、サービス内の InstanceContext オブジェクトの最大数を指定します。

これらのプロパティの設定は、負荷の下でアプリケーションを実際に実行してみた後に決定することが多いため、ServiceThrottlingBehavior プロパティの設定は、通常、<serviceThrottling> 要素を使用してアプリケーション構成ファイルに指定します。

簡単な例として、ServiceThrottlingBehaviorMaxConcurrentSessions、および MaxConcurrentCalls の各プロパティを 1 に設定するアプリケーション構成ファイルから MaxConcurrentInstances クラスを使用する方法を次のコード例に示します。 特定のアプリケーションでの最適な設定については、実際の動作から判断します。

<configuration>
  <appSettings>
    <!-- use appSetting to configure base address provided by host -->
    <add key="baseAddress" value="http://localhost:8080/ServiceMetadata" />
  </appSettings>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="Throttled" >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
         />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
         />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior  name="Throttled">
          <serviceThrottling 
            maxConcurrentCalls="1" 
            maxConcurrentSessions="1" 
            maxConcurrentInstances="1"
          />
          <serviceMetadata 
            httpGetEnabled="true" 
            httpGetUrl=""
          />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

実行時における正確な動作は ConcurrencyMode プロパティと InstanceContextMode プロパティの値により異なります。この値により、操作内で一度に実行できるメッセージ数や、受信チャネル セッションに関連するサービス InstanceContext の有効期間をそれぞれ制限します。

詳細については、「MaxConcurrentCalls」および「MaxConcurrentInstances」を参照してください。

関連項目