This topic has not yet been rated - Rate this topic

OperationContext Class

Provides access to the execution context of a service method.

System.Object
  System.ServiceModel.OperationContext

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
public sealed class OperationContext : IExtensibleObject<OperationContext>

The OperationContext type exposes the following members.

  Name Description
Public method Supported by Portable Class Library OperationContext Initializes a new instance of the OperationContext class that uses the specified IContextChannel in a client application.
Top
  Name Description
Public property Channel Gets the channel associated with the current OperationContext object.
Public property Static member Supported by Portable Class Library Current Gets or sets the execution context for the current thread.
Public property EndpointDispatcher Gets or sets the endpoint dispatcher for the endpoint to inspect or modify.
Public property Extensions Gets the collection of service extensions from the current message context.
Public property HasSupportingTokens Gets a value that indicates whether the incoming message has supporting tokens.
Public property Host Gets the ServiceHost for the current service object.
Public property Supported by Portable Class Library IncomingMessageHeaders Gets the incoming message headers for the OperationContext.
Public property Supported by Portable Class Library IncomingMessageProperties Gets the message properties for the incoming message in the OperationContext.
Public property Supported by Portable Class Library IncomingMessageVersion Gets the incoming SOAP message version for the OperationContext.
Public property InstanceContext Gets the InstanceContext object that manages the current service instance.
Public property Supported by Portable Class Library IsUserContext This property is intended for system use and should not be called by users.
Public property Supported by Portable Class Library OutgoingMessageHeaders Gets the outgoing message headers for the active OperationContext.
Public property Supported by Portable Class Library OutgoingMessageProperties Gets the message properties for the outbound message in the active OperationContext.
Public property Supported by Portable Class Library RequestContext Gets or sets the RequestContext implementation for this method.
Public property ServiceSecurityContext Gets or sets the ServiceSecurityContext within which this method executes.
Public property SessionId Gets the String used to identify the current session.
Public property SupportingTokens Gets a ICollection<T> of type System.IdentityModel.Tokens.SecurityToken.
Top
  Name Description
Public method Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetCallbackChannel<T> Gets a channel to the client instance that called the current operation.
Public method Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetTransactionComplete Commits the currently executing transaction.
Public method Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event Supported by Portable Class Library OperationCompleted Occurs when the operation has completed.
Top

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
{
  [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 Note

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Be aware that this is ThreadStatic
Please be aware that the OperationContext.Current reference is ThreadStatic...if you spin a Task or background thread while servicing a WCF operation, OperationContext.Current will be null.