OperationContext Class
Assembly: System.ServiceModel (in system.servicemodel.dll)
'Declaration Public NotInheritable Class OperationContext Implements IExtensibleObject(Of OperationContext) 'Usage Dim instance As OperationContext
public final class OperationContext implements IExtensibleObject<OperationContext>
public final class OperationContext implements IExtensibleObject<OperationContext>
Not applicable.
Use the OperationContext from within a service operation to access the current operation execution environment. In particular, the operation context is used to access callback channels in duplex services, to store extra state data across portions of the operations, and to access incoming message headers and properties as well as add outgoing message headers and properties.
For more information about using extensions to store state data, see Extensible Objects.
The OperationContext has the following properties and methods.
-
The Current property returns the OperationContext object representing the current execution context.
-
The ServiceSecurityContext property returns the security environment under which the method executes.
-
The EndpointDispatcher property gets the operation's System.ServiceModel.Dispatcher.EndpointDispatcher.
-
The Extensions property returns an extension collection for the current OperationContext.
-
The Host property returns the ServiceHostBase object that manages the service.
-
The HasSupportingTokens property returns a value that indicates whether the method has supporting tokens, if so, the SupportingTokens property gets them.
-
The IncomingMessageHeaders, IncomingMessageProperties, and IncomingMessageVersion properties get these items from the incoming message.
-
The OperationCompleted event is fired when the operation has completed.
-
The OutgoingMessageHeaders and OutgoingMessageProperties properties get these items for the outbound message.
-
The RequestContext property returns the RequestContext implementation for the method.
-
The InstanceContext property returns the InstanceContext associated with the operation.
-
The SessionId property returns the session identifier for the current channel and object.
-
The GetCallbackChannel method returns a callback channel to the caller in the case of duplex communication.
-
The SetTransactionComplete method commits the current transaction.
The following code example uses the Current property and GetCallbackChannel method to obtain the 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); } } }
The following client implements the SampleDuplexHelloCallback to receive the callback message. The imported callback contract is not the same name as the one in the service, due to the use of the Name property in the preceding example. Note that the client makes no assumptions about whether or when it might receive a callback; the server callback is entirely independent of the client's outbound call.
Note: |
|---|
| For an example that uses the OperationContext class in a client scenario, see OperationContextScope. |
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: