OperationContext 類別

定義

提供服務方法執行內容的存取。

C#
public sealed class OperationContext : System.ServiceModel.IExtensibleObject<System.ServiceModel.OperationContext>
繼承
OperationContext
實作

範例

下列程式碼範例會 Current 使用 屬性和 GetCallbackChannel 方法,從 方法內取得呼叫端的通道。 本範例的所有方法都是單向方法,可讓服務與用戶端獨立地進行雙向通訊。 在此情況下,範例用戶端應用程式在結束前只預期一個傳回呼叫,但另一個用戶端,例如Windows Forms用戶端可以從服務接收任意數目的呼叫。

C#
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Name = "SampleDuplexHello",
    Namespace = "http://microsoft.wcf.documentation",
    CallbackContract = typeof(IHelloCallbackContract),
    SessionMode = SessionMode.Required
  )]
  public interface IDuplexHello
  {
    [OperationContract(IsOneWay = true)]
    void Hello(string greeting);
  }

  public interface IHelloCallbackContract
  {
    [OperationContract(IsOneWay = true)]
    void Reply(string responseToGreeting);
  }

  public class DuplexHello : IDuplexHello
  {
    public DuplexHello()
    {
      Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
    }

    ~DuplexHello()
    {
      Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
    }

    public void Hello(string greeting)
    {
      Console.WriteLine("Caller sent: " + greeting);
      Console.WriteLine("Session ID: " + OperationContext.Current.SessionId);
      Console.WriteLine("Waiting two seconds before returning call.");
      // Put a slight delay to demonstrate asynchronous behavior on client.
      Thread.Sleep(2000);
      IHelloCallbackContract callerProxy
        = OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
      string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
      Console.WriteLine("Sending back: " + response);
      callerProxy.Reply(response);
    }
  }
}

下列用戶端會實作 SampleDuplexHelloCallback 來接收回呼訊息。 由於使用上述範例中的 屬性, Name 匯入的回呼合約與服務中的回呼合約名稱不同。 注意,用戶端並沒有針對是否接收到回呼,或何時接收到回呼作任何的假設;伺服器回呼與用戶端的傳出呼叫完全無關。

備註

如需在 OperationContext 用戶端案例中使用 類別的範例,請參閱 OperationContextScope

C#
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  public class Client : SampleDuplexHelloCallback
  {
    AutoResetEvent waitHandle;

    public Client()
    {
      waitHandle = new AutoResetEvent(false);
    }

    public void Run()
    {
      // Picks up configuration from the config file.
      SampleDuplexHelloClient wcfClient
        = new SampleDuplexHelloClient(new InstanceContext(this));
      try
      {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Enter a greeting to send and press ENTER: ");
        Console.Write(">>> ");
        Console.ForegroundColor = ConsoleColor.Green;
        string greeting = Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Called service with: \r\n\t" + greeting);
        wcfClient.Hello(greeting);
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
        this.waitHandle.WaitOne();
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Set was called.");
        Console.Write("Press ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("ENTER");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write(" to exit...");
        Console.ReadLine();
        wcfClient.Close();
      }
      catch (TimeoutException timeProblem)
      {
        Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        Console.ReadLine();
        wcfClient.Abort();
      }
      catch (CommunicationException commProblem)
      {
        Console.WriteLine("There was a communication problem. " + commProblem.Message);
        Console.ReadLine();
        wcfClient.Abort();
      }
    }

    public static void Main()
    {
      Client client = new Client();
      client.Run();
    }

    public void Reply(string response)
    {
      Console.WriteLine("Received output.");
      Console.WriteLine("\r\n\t" + response);
      this.waitHandle.Set();
    }
  }
}

備註

OperationContext使用服務作業內的 來存取目前的作業執行環境。 作業內容特別用來存取雙工服務中的回呼通道、儲存作業各部分的額外狀態資料、存取傳入訊息標頭與屬性,以及新增傳出訊息標頭與屬性。

如需使用擴充功能來儲存狀態資料的詳細資訊,請參閱 可延伸物件

OperationContext具有下列屬性和方法。

建構函式

OperationContext(IContextChannel)

使用指定之用戶端應用程式中的 OperationContext來初始化 IContextChannel 類別的新執行個體。

屬性

Channel

取得與目前 OperationContext 物件相關聯的通道。

ClaimsPrincipal

取得與作業相關聯的宣告架構主體。

Current

取得或設定目前執行緒的執行內容。

EndpointDispatcher

取得或設定要檢查或修改的端點之端點發送器。

Extensions

從目前訊息內容取得服務擴充部分的集合。

HasSupportingTokens

取得值,這個值表示傳入訊息是否支援權杖。

Host

取得目前服務物件的 ServiceHost

IncomingMessageHeaders

取得 OperationContext 的傳入訊息標頭。

IncomingMessageProperties

取得 OperationContext 中傳入訊息的訊息屬性。

IncomingMessageVersion

取得 OperationContext 的傳入 SOAP 訊息版本。

InstanceContext

取得管理目前服務執行個體的 InstanceContext 物件。

IsUserContext

本屬性僅供系統使用,使用者不應呼叫。

OutgoingMessageHeaders

取得使用中 OperationContext 的傳出訊息標頭。

OutgoingMessageProperties

取得使用中 OperationContext 內的傳出訊息之訊息屬性。

RequestContext

取得或設定本方法的 RequestContext 實作。

ServiceSecurityContext

取得或設定 ServiceSecurityContext,此方法會在其中執行。

SessionId

取得用來識別目前工作階段的 String

SupportingTokens

取得 ICollection<T> 型別的 SecurityToken

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetCallbackChannel<T>()

取得至用戶端執行個體的通道,此通道呼叫了目前作業。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
SetTransactionComplete()

認可目前執行的異動。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

事件

OperationCompleted

發生於作業已經完成時。

適用於

產品 版本
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
UWP 10.0