Language

OperationContext.Current プロパティ

定義

現在のスレッドの実行コンテキストを取得または設定します。

public:
 static property System::ServiceModel::OperationContext ^ Current { System::ServiceModel::OperationContext ^ get(); void set(System::ServiceModel::OperationContext ^ value); };
public static System.ServiceModel.OperationContext Current { get; set; }
static member Current : System.ServiceModel.OperationContext with get, set
Public Shared Property Current As OperationContext

プロパティ値

現在のメソッドのメッセージングコンテキストと実行コンテキストを表す OperationContext 。

例

次のコード例では、 Current プロパティと GetCallbackChannel メソッドを使用して、メソッド内から呼び出し元にチャネルを作成します。 この例のすべてのメソッドは一方向のメソッドであり、サービスとクライアントが双方向に独立して通信できるようにします。 この場合、クライアント アプリケーションの例では、終了する前に 1 回のリターン呼び出ししか想定しませんが、別のクライアント (Windows フォーム クライアントなど) は、サービスから任意の数の呼び出しを受信できます。

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

注釈

Current プロパティを使用して、現在のメソッドの実行コンテキストとメッセージ コンテキストを取得します。

適用対象