共用方式為


ConcurrencyMode 列舉

定義

指定服務類別是支援單一執行緒或多重執行緒的作業模式。

public enum class ConcurrencyMode
public enum ConcurrencyMode
type ConcurrencyMode = 
Public Enum ConcurrencyMode
繼承
ConcurrencyMode

欄位

Multiple 2

服務執行個體為多執行緒。 不保證同步處理。 由於其他執行緒可隨時變更您的服務物件,您必須隨時處理同步及狀態的一致性。

Reentrant 1

服務執行個體為單一執行緒,且接受重新進入呼叫。 重新進入服務在您呼叫其他服務時接受呼叫;因此您的責任是在 Callout 前保持物件狀態一致性,並且必須確定作業本機資料在 Callout 後有效。 請注意,只有透過 WCF 通道呼叫另一個服務才能解除服務執行個體鎖定。 此時,被呼叫的服務可透過回呼重新進入第一個服務。 如果第一個服務無法重新進入,這時的呼叫順序會造成死結。 如需詳細資訊,請參閱 ConcurrencyMode

Single 0

該服務執行個體為單一執行緒服務,而且不接受重新進入 (Reentrant) 的呼叫。 若 InstanceContextMode 屬性為 Single,而且在執行個體處理呼叫時有另外的訊息抵達,這些訊息必須等到服務可使用,或者訊息逾時為止。

範例

下列程式碼範例示範使用 Single、Reentrant 和 Multiple 之間的差異。 此範例不會在沒有實際實作後方進行編譯,但會示範 WCF 所建立的執行緒類型,以及對您的作業程式碼的意義。

using System;
using System.ServiceModel;

[ServiceContract]
public interface IHttpFetcher
{
  [OperationContract]
  string GetWebPage(string address);
}

// These classes have the invariant that:
//     this.slow.GetWebPage(this.cachedAddress) == this.cachedWebPage.
// When you read cached values you can assume they are valid. When
// you write the cached values, you must guarantee that they are valid.
// With ConcurrencyMode.Single, WCF does not call again into the object
// so long as the method is running. After the operation returns the object
// can be called again, so you must make sure state is consistent before
// returning.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
class SingleCachingHttpFetcher : IHttpFetcher
{
    string cachedWebPage;
    string cachedAddress;
    readonly IHttpFetcher slow;

    public string GetWebPage(string address)
    {
        // <-- Can assume cache is valid.
        if (this.cachedAddress == address)
        {
            return this.cachedWebPage;
        }

        // <-- Cache is no longer valid because we are changing
        // one of the values.
        this.cachedAddress = address;
        string webPage = slow.GetWebPage(address);
        this.cachedWebPage = webPage;
        // <-- Cache is valid again here.

        return this.cachedWebPage;
        // <-- Must guarantee that the cache is valid because we are returning.
    }
}

// With ConcurrencyMode.Reentrant, WCF makes sure that only one
// thread runs in your code at a time. However, when you call out on a
// channel, the operation can get called again on another thread. Therefore
// you must confirm that state is consistent both before channel calls and
// before you return.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
class ReentrantCachingHttpFetcher : IHttpFetcher
{
  string cachedWebPage;
  string cachedAddress;
  readonly SlowHttpFetcher slow;

  public ReentrantCachingHttpFetcher()
  {
    this.slow = new SlowHttpFetcher();
  }

  public string GetWebPage(string address)
  {
    // <-- Can assume that cache is valid.
    if (this.cachedAddress == address)
    {
        return this.cachedWebPage;
    }

    // <-- Must guarantee that the cache is valid, because
    // the operation can be called again before we return.
    string webPage = slow.GetWebPage(address);
    // <-- Can assume cache is valid.

    // <-- Cache is no longer valid because we are changing
    // one of the values.
    this.cachedAddress = address;
    this.cachedWebPage = webPage;
    // <-- Cache is valid again here.

    return this.cachedWebPage;
    // <-- Must guarantee that cache is valid because we are returning.
  }
}

// With ConcurrencyMode.Multiple, threads can call an operation at any time.
// It is your responsibility to guard your state with locks. If
// you always guarantee you leave state consistent when you leave
// the lock, you can assume it is valid when you enter the lock.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
class MultipleCachingHttpFetcher : IHttpFetcher
{
  string cachedWebPage;
  string cachedAddress;
  readonly SlowHttpFetcher slow;
  readonly object ThisLock = new object();

  public MultipleCachingHttpFetcher()
  {
    this.slow = new SlowHttpFetcher();
  }

  public string GetWebPage(string address)
  {
    lock (this.ThisLock)
    {
      // <-- Can assume cache is valid.
      if (this.cachedAddress == address)
      {
          return this.cachedWebPage;
          // <-- Must guarantee that cache is valid because
          // the operation returns and releases the lock.
      }
      // <-- Must guarantee that cache is valid here because
      // the operation releases the lock.
    }

    string webPage = slow.GetWebPage(address);

    lock (this.ThisLock)
    {
      // <-- Can assume cache is valid.

      // <-- Cache is no longer valid because the operation
      // changes one of the values.
      this.cachedAddress = address;
      this.cachedWebPage = webPage;
      // <-- Cache is valid again here.

      // <-- Must guarantee that cache is valid because
      // the operation releases the lock.
    }

    return webPage;
  }
}

備註

ConcurrencyModeConcurrencyMode 屬性搭配使用,指定服務類別是支援單一執行緒或多重執行緒的作業模式。 單一執行緒作業可為可重新進入 (Reentrant) 或不可重新進入 (Non-Reentrant)。

下表顯示 Windows Communication Foundation (WCF) 允許在另一個作業進行時叫用作業,視 而定 ConcurrencyMode

ConcurrencyMode 值 新作業可叫用嗎?
Single 永不。
折返 只在叫用另一服務或回呼時。
多個 永遠。

適用於