次の方法で共有


ServiceBehaviorAttribute.ConcurrencyMode プロパティ

定義

サービスが 1 つのスレッド、複数のスレッド、または再入呼び出しをサポートするかどうかを取得または設定します。

public:
 property System::ServiceModel::ConcurrencyMode ConcurrencyMode { System::ServiceModel::ConcurrencyMode get(); void set(System::ServiceModel::ConcurrencyMode value); };
public System.ServiceModel.ConcurrencyMode ConcurrencyMode { get; set; }
member this.ConcurrencyMode : System.ServiceModel.ConcurrencyMode with get, set
Public Property ConcurrencyMode As ConcurrencyMode

プロパティ値

ConcurrencyMode値の 1 つ。既定値は Single です。

例外

値は、 ConcurrencyMode 値の 1 つではありません。

次のコード例は、 SingleReentrantMultipleの使用の違いを示しています。 このサンプルは、実際の実装なしでコンパイルするわけではありませんが、Windows Communication Foundation (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;
  }
}

注釈

このプロパティは、サービスのインスタンスが同時に実行される 1 つのスレッドまたは複数のスレッドを処理できるかどうかを示し、シングル スレッドの場合は再入がサポートされているかどうかを示します。

ConcurrencyMode プロパティは、他の設定とやり取りします。 たとえば、 InstanceContextMode 値が Single に設定されている場合、 ConcurrencyMode 値を Multiple に設定しない限り、サービスは一度に 1 つのメッセージしか処理できません。 また、このプロパティは、 ServiceContractAttribute.SessionMode プロパティと組み合わせて動作を生成します。 詳細については、「 セッション、インスタンス化、コンカレンシー」を参照してください。

ConcurrencyModeSingle に設定すると、サービスのインスタンスを一度に 1 つの実行スレッドに制限するようにシステムに指示されます。これにより、スレッドの問題に対処できなくなります。 Multiple値は、サービス オブジェクトを一度に複数のスレッドで実行できることを意味します。 この場合は、スレッド セーフを確保する必要があります。

Reentrant また、一度に 1 つのスレッドへのアクセスを制限します。操作の処理中に、他のメッセージが操作に入る可能性はありません。 操作中に別のサービスへの呼び出しが終了すると、現在のメッセージは操作のロックを失い、他のメッセージを自由に処理できます。 サービス呼び出しが戻ると、ロックが再確立され、元のメッセージは、その結論まで、または操作の別の呼び出しが発生するまで処理を続行できます。

Important

Singleはサービスのインスタンスを一度に 1 つの実行スレッドに制限しますが、MaxConcurrentCallsを 1 に設定して、順序が正しくないことを保証する必要もあります。

また、吹き出しの前にオブジェクトの状態を一貫性を保ち、操作ローカル データが吹き出しの後で有効であることを確認する必要があります。 サービス インスタンスは、WCF チャネル経由で別のサービスを呼び出すことによってのみロック解除されることに注意してください。 この場合、呼び出されたサービスはコールバックを介して最初のサービスを再入力できます。 最初のサービスが再入可能でない場合、呼び出しのシーケンスによってデッドロックが発生します。 詳細については、 ConcurrencyModeを参照してください。

処理操作からの送信呼び出し中に、操作に対してローカルではないデータを変更できます。 (ローカル状態データは、元のメッセージが処理を再開するときに有効であることが保証されます)。その結果、発信呼び出しの前に、ローカル以外のデータが他の着信呼び出しで有効であることを確認し、発信呼び出しが戻った後にローカル以外のデータを再検証する必要があります。

次の擬似コードは、再入サポートを成功させるために必要なパターンを示しています。

public void MyMethod()
{
  this.SomeNonLocalDataState;
  // Here you need to clean nonlocal state for other users
  OutboundProxy proxy = new OutboundProxy();
  int returnValue = proxy.CallOutOfOperation();
  // Ensure that this.SomeNonLocalDataState is valid for continued use.
  this.ModifyNonLocalState;
  return returnValue;
}

ConcurrencyModeReentrantされたときに発信呼び出しに Begin/End 非同期呼び出しパターンを使用すると、例外がトリガーされます。 非同期送信呼び出しでは、 ConcurrencyModeMultipleされる操作が必要です。その場合は、同期の問題を処理する必要があります。

一般に、コンカレンシー モードに違反するインスタンスにメッセージが到着した場合、メッセージはインスタンスが使用可能になるまで、またはタイムアウトするまで待機します。

さらに、 ConcurrencyModeSingle に設定されていて、インスタンスが解放されるのを待っている間に再入呼び出しがブロックされた場合、システムはデッドロックを検出し、例外をスローします。

ConcurrencyMode プロパティが Single に設定されているときにReleaseServiceInstanceOnTransactionCompletetrueされると、実行時にInvalidOperationExceptionがスローされます。

OperationBehaviorAttribute.TransactionScopeRequiredが true に設定されている操作があり、ConcurrencyModeReentrant に設定した場合は、ReleaseServiceInstanceOnTransactionCompleteを明示的にfalseに設定する必要があることに注意してください。 それ以外の場合は、 ReleaseServiceInstanceOnTransactionComplete の既定値が trueされているため、検証例外がスローされます。

ランタイムの動作を変更できる ConcurrencyMode とその他のプロパティの相互作用があります。 これらの相互作用の詳細については、「 セッション、インスタンス化、コンカレンシー」を参照してください。

適用対象