This documentation is archived and is not being maintained.

OperationContext Class

Provides access to the execution context of a service method.


Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public sealed class OperationContext : IExtensibleObject<OperationContext>

The OperationContext type exposes the following members.

Public methodSupported by Portable Class LibraryOperationContextInitializes a new instance of the OperationContext class that uses the specified IContextChannel in a client application.

Public propertyChannelGets the channel associated with the current OperationContext object.
Public propertyStatic memberSupported by Portable Class LibraryCurrentGets or sets the execution context for the current thread.
Public propertyEndpointDispatcherGets or sets the endpoint dispatcher for the endpoint to inspect or modify.
Public propertyExtensionsGets the collection of service extensions from the current message context.
Public propertyHasSupportingTokensGets a value that indicates whether the incoming message has supporting tokens.
Public propertyHostGets the ServiceHost for the current service object.
Public propertySupported by Portable Class LibraryIncomingMessageHeadersGets the incoming message headers for the OperationContext.
Public propertySupported by Portable Class LibraryIncomingMessagePropertiesGets the message properties for the incoming message in the OperationContext.
Public propertySupported by Portable Class LibraryIncomingMessageVersionGets the incoming SOAP message version for the OperationContext.
Public propertyInstanceContextGets the InstanceContext object that manages the current service instance.
Public propertySupported by Portable Class LibraryIsUserContextThis property is intended for system use and should not be called by users.
Public propertySupported by Portable Class LibraryOutgoingMessageHeadersGets the outgoing message headers for the active OperationContext.
Public propertySupported by Portable Class LibraryOutgoingMessagePropertiesGets the message properties for the outbound message in the active OperationContext.
Public propertySupported by Portable Class LibraryRequestContextGets or sets the RequestContext implementation for this method.
Public propertyServiceSecurityContextGets or sets the ServiceSecurityContext within which this method executes.
Public propertySessionIdGets the String used to identify the current session.
Public propertySupportingTokensGets a ICollection<T> of type System.IdentityModel.Tokens.SecurityToken.

Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetCallbackChannel<T>Gets a channel to the client instance that called the current operation.
Public methodSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetTransactionCompleteCommits the currently executing transaction.
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)

Public eventSupported by Portable Class LibraryOperationCompletedOccurs when the operation has completed.

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 following code example uses the Current property and GetCallbackChannel<T> 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
    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());

      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.
      IHelloCallbackContract callerProxy
        = OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>();
      string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting;
      Console.WriteLine("Sending back: " + 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.


For an example that uses the OperationContext class in a client scenario, see OperationContextScope.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.