Bagikan melalui


ConcurrencyMode Enum

Definisi

Menentukan apakah kelas layanan mendukung mode operasi berulir tunggal atau multi-utas.

public enum class ConcurrencyMode
public enum ConcurrencyMode
type ConcurrencyMode = 
Public Enum ConcurrencyMode
Warisan
ConcurrencyMode

Bidang

Nama Nilai Deskripsi
Single 0

Instans layanan berutas tunggal dan tidak menerima panggilan masuk kembali. InstanceContextMode Jika properti adalah Single, dan pesan tambahan tiba saat instans melayani panggilan, pesan ini harus menunggu hingga layanan tersedia atau sampai pesan habis.

Reentrant 1

Instans layanan berutas tunggal dan menerima panggilan masuk kembali. Layanan masuk kembali menerima panggilan saat Anda memanggil layanan lain; oleh karena itu, Anda bertanggung jawab untuk membiarkan status objek Anda konsisten sebelum panggilan dan Anda harus mengonfirmasi bahwa data operasi-lokal valid setelah panggilan. Perhatikan bahwa instans layanan tidak terkunci hanya dengan memanggil layanan lain melalui saluran WCF. Dalam hal ini, layanan yang disebut dapat memasukkan kembali layanan pertama melalui panggilan balik. Jika layanan pertama tidak masuk kembali, urutan panggilan menghasilkan kebuntuan. Untuk detailnya, lihat ConcurrencyMode.

Multiple 2

Instans layanan multi-utas. Tidak ada jaminan sinkronisasi yang dibuat. Karena utas lain dapat mengubah objek layanan Anda kapan saja, Anda harus menangani sinkronisasi dan konsistensi status setiap saat.

Contoh

Contoh kode berikut menunjukkan perbedaan antara menggunakan Single, Reentrant, dan Multiple. Sampel ini tidak dikompilasi tanpa implementasi nyata di belakangnya, tetapi menunjukkan jenis utas menjamin bahwa WCF membuat dan apa artinya untuk kode operasi Anda.

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

Keterangan

ConcurrencyMode digunakan bersama dengan ConcurrencyMode properti untuk menentukan apakah kelas layanan mendukung mode operasi berulir tunggal atau multi-utas. Operasi utas tunggal dapat masuk kembali atau tidak masuk kembali.

Tabel berikut menunjukkan kapan Windows Communication Foundation (WCF) mengizinkan operasi dipanggil saat operasi lain sedang berlangsung, tergantung pada ConcurrencyMode.

Nilai ConcurrencyMode Dapatkah operasi baru dipanggil?
Single Pernah.
Reentrant Hanya saat memanggil layanan lain atau panggilan balik.
Beberapa Selalu.

Berlaku untuk