ConcurrencyMode 열거형

정의

서비스 클래스가 단일 스레드 작업 모드를 지원할지 다중 스레드 작업 모드를 지원할지를 지정합니다.

public enum class ConcurrencyMode
public enum ConcurrencyMode
type ConcurrencyMode = 
Public Enum ConcurrencyMode
상속
ConcurrencyMode

필드

Multiple 2

서비스 인스턴스가 다중 스레드입니다. 동기화가 보장되지 않습니다. 다른 스레드가 언제든지 서비스 개체를 변경할 수 있으므로 항상 동기화 및 상태 일관성을 처리해야 합니다.

Reentrant 1

서비스 인스턴스가 단일 스레드이며 재진입 호출을 허용합니다. 재진입 서비스는 사용자가 다른 서비스를 호출할 때 호출을 허용합니다. 따라서 사용자는 호출 전에 개체 상태의 일관성을 유지하고 호출 후에는 작업-로컬 데이터가 유효한지 확인해야 합니다. 서비스 인스턴스는 WCF 채널을 통해 다른 서비스를 호출하는 방법으로만 잠금 해제됩니다. 이런 경우 호출된 서비스는 콜백을 통해 첫 번째 서비스에 재진입할 수 있습니다. 첫 번째 서비스가 재진입이 아닌 경우 호출 시퀀스는 교착 상태에 빠지게 됩니다. 자세한 내용은 ConcurrencyMode를 참조하십시오.

Single 0

서비스 인스턴스가 단일 스레드이며 재진입 호출을 허용하지 않습니다. InstanceContextMode 속성이 Single이고 인스턴스가 호출을 처리하는 동안 추가 메시지가 도착하는 경우, 이 추가 메시지는 서비스가 사용 가능해지거나 메시지 제한 시간이 초과될 때까지 대기해야 합니다.

예제

다음 코드 예제에서는 단일, 재진입, 및 여러 사용 간의 차이점을 보여 줍니다. 이 샘플 뒤에 실제 구현이 없어 컴파일되지 않습니다 하지만 보장 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 속성과 함께 사용합니다. 단일 스레드 작업은 재진입이거나 재진입이 아닐 수 있습니다.

다음 표에서 경우 Windows Communication Foundation (WCF) 허용, 진행 중인 다른 작업에 따라는 ConcurrencyMode합니다.

ConcurrencyMode 값 새 작업 호출 가능 여부
Single 불가능
재진입 다른 서비스나 콜백 호출 도중에만 가능
여러 항상

적용 대상