OperationContext.Current Property
Gets or sets the execution context for the current thread.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Property Value
Type: System.ServiceModel.OperationContextThe OperationContext that represents the messaging and execution context of the current method.
The following code example uses the Current property and GetCallbackChannel<T> method to create a channel back to the caller from within a method. All methods in this example are one-way methods, enabling the service and the client to communicate in both directions independently. In this case, the example client application expects only one return call before it exits, but another client, for example a Windows Forms client, can receive any number of calls from the service.
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); } } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.