次の方法で共有


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

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 をそれぞれ制御します。

詳細については、 MaxConcurrentCallsMaxConcurrentInstancesに関するページを参照してください。

こちらも参照ください