共用方式為


使用 ServiceThrottlingBehavior 來控制 WCF 服務效能

類別 ServiceThrottlingBehavior 會公開屬性,您可以用來限制在應用層級建立的實例或會話數目。 使用此行為,您可以微調 Windows Communication Foundation (WCF) 應用程式的效能。

控制服務實例和並行呼叫

使用MaxConcurrentCalls屬性可指定ServiceHost類別中主動處理的訊息數量上限,使用MaxConcurrentInstances屬性可指定服務中InstanceContext物件的數量上限。

由於這些屬性的設定通常是在應用程式對載入進行的實際運行經驗之後才決定,因此通常在應用程式的組態檔中使用 ServiceThrottlingBehavior 元素來指定這些<屬性的設定。

下列程式代碼範例示範從應用程式組態檔使用 ServiceThrottlingBehavior 類別,將、 MaxConcurrentSessionsMaxConcurrentCalls 屬性設定MaxConcurrentInstances為 1 做為簡單範例。 真實世界體驗會決定任何特定應用程式的最佳設定。

<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

另請參閱