Share via


ServiceThrottlingBehavior.MaxConcurrentSessions 屬性

定義

取得或設定值,指定 ServiceHost 物件一次可以接受的工作階段數目上限。

public:
 property int MaxConcurrentSessions { int get(); void set(int value); };
public int MaxConcurrentSessions { get; set; }
member this.MaxConcurrentSessions : int with get, set
Public Property MaxConcurrentSessions As Integer

屬性值

Int32

服務主機接受工作階段的數目上限。 預設值為處理器計數的 100 倍。

範例

下列程式碼範例會示範如何使用應用程式組態檔的 ServiceThrottlingBehavior,將 MaxConcurrentSessionsMaxConcurrentCallsMaxConcurrentInstances 屬性設定為 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>

備註

MaxConcurrentSessions 屬性會指定 ServiceHost 物件可以接受的工作階段數目上限。 請務必了解,這個情況中的工作階段不僅是支援可靠工作階段的通道 (例如,System.ServiceModel.NetNamedPipeBinding 支援工作階段,但不包括可靠的工作階段)。

每個接聽程式物件可以有一個擱置的通道會話,在 WCF 接受通道會話並開始處理訊息之前,不會計入 的值 MaxConcurrentSessions 。 這個屬性在運用工作階段的案例中最為有用。

當這個屬性設定為小於用戶端執行緒數目的值時,來自多個用戶端的要求可能會在相同通訊端連線中形成佇列。 如果服務上的開啟工作階段數量已達到 MaxConcurrentSessions,來自尚未以服務建立工作階段之用戶端的要求將遭到封鎖,直到服務以其他用戶端關閉其工作階段為止。 未處理的用戶端要求會逾時,而且服務會突然關閉工作階段。

若要避免這種情況,請從不同的應用程式定義域執行用戶端執行緒,如此,要求訊息就可以進入不同的通訊端連線。

您也可以在應用程式組態檔中使用 < serviceThrottling > 元素來設定此屬性的值。

適用於