ConcurrencyMode ConcurrencyMode ConcurrencyMode ConcurrencyMode Enum

Definizione

Specifica se una classe del servizio supporta modalità operative a thread singolo o multithread.Specifies whether a service class supports single-threaded or multi-threaded modes of operation.

public enum class ConcurrencyMode
public enum ConcurrencyMode
type ConcurrencyMode = 
Public Enum ConcurrencyMode
Ereditarietà
ConcurrencyModeConcurrencyModeConcurrencyModeConcurrencyMode

Campi

Multiple Multiple Multiple Multiple 2

L'istanza del servizio è multithread.The service instance is multi-threaded. Non viene resa alcuna garanzia di sincronizzazione.No synchronization guarantees are made. Poiché è possibile che altri thread modifichino l'oggetto servizio in qualsiasi momento, è necessario gestire sempre la sincronizzazione e la coerenza dello stato.Because other threads can change your service object at any time, you must handle synchronization and state consistency at all times.

Reentrant Reentrant Reentrant Reentrant 1

L'istanza del servizio è a thread singolo e accetta chiamate rientranti.The service instance is single-threaded and accepts reentrant calls. Il servizio rientrante accetta chiamate quando viene chiamato un altro servizio; è pertanto responsabilità dello sviluppatore garantire la coerenza dello stato dell'oggetto prima dei callout ed è necessario confermare che i dati locali dell'operazione siano validi dopo i callout.The reentrant service accepts calls when you call another service; it is therefore your responsibility to leave your object state consistent before callouts and you must confirm that operation-local data is valid after callouts. Si noti che l'istanza del servizio viene sbloccata solo chiamando un altro servizio su un canale WCF.Note that the service instance is unlocked only by calling another service over a WCF channel. In questo caso, il servizio chiamato può rientrare nel primo servizio tramite un callback.In this case, the called service can reenter the first service via a callback. Se il primo servizio non è rientrante, la sequenza di chiamate determina un deadlock.If the first service is not reentrant, the sequence of calls results in a deadlock. Per informazioni dettagliate, vedere ConcurrencyMode.For details, see ConcurrencyMode.

Single Single Single Single 0

L'istanza del servizio è a thread singolo e non accetta chiamate rientranti.The service instance is single-threaded and does not accept reentrant calls. Se la proprietà InstanceContextMode è Single e giungono altri messaggi mentre l'istanza elabora una chiamata, tali messaggi restano in attesa fino a quando il servizio non diventa disponibile o fino al loro timeout.If the InstanceContextMode property is Single, and additional messages arrive while the instance services a call, these messages must wait until the service is available or until the messages time out.

Esempi

Esempio di codice seguente vengono illustrate le differenze tra l'utilizzo singolo rientrante e più.The following code example demonstrates the different between using Single, Reentrant, and Multiple. In questo esempio non viene compilato senza un'effettiva implementazione sottostante, ma illustrano il tipo del threading in modo che WCF rende e cosa significa per il codice dell'operazione.This sample does not compile without a real implementation behind it, but does demonstrate the kind of threading guarantees that WCF makes and what that means for your operation code.

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;
  }
}

Commenti

L'enumerazione ConcurrencyMode viene utilizzata insieme alla proprietà ConcurrencyMode per specificare se una classe del servizio supporta modalità operative a thread singolo o multithread.ConcurrencyMode is used in conjunction with the ConcurrencyMode property to specify whether a service class supports single-threaded or multi-threaded modes of operation. Un'operazione a thread singolo può essere rientrante o non rientrante.A single-threaded operation can be either reentrant or non-reentrant.

La tabella seguente illustra quando Windows Communication Foundation (WCF) consente un'operazione da richiamare durante un'altra è in corso, a seconda di ConcurrencyMode.The following table shows when Windows Communication Foundation (WCF) permits an operation to be invoked while another one is in progress, depending upon the ConcurrencyMode.

Valore di ConcurrencyModeConcurrencyMode Value Possibilità di richiamare una nuova operazioneCan a new operation be invoked?
SingleSingle MaiNever.
RientranteReentrant Solo durante la chiamata di un altro servizio o un callbackOnly while invoking another service or a callback.
Selezione multiplaMultiple SempreAlways.

Si applica a