Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

OperationContextScope Class

Creates a block within which an OperationContext object is in scope.


Namespace:  System.ServiceModel
Assemblies:   System.ServiceModel.Primitives (in System.ServiceModel.Primitives.dll)
  System.ServiceModel (in System.ServiceModel.dll)

public sealed class OperationContextScope : IDisposable

The OperationContextScope type exposes the following members.

Public methodSupported by Portable Class LibraryOperationContextScope(IContextChannel)Initializes a new instance of the OperationContextScope class that uses the specified IContextChannel to create a new OperationContext for the scope.
Public methodSupported by Portable Class LibraryOperationContextScope(OperationContext)Initializes a new instance of the OperationContextScope class to create a scope for the specified OperationContext object.

Public methodSupported by Portable Class LibraryDisposeRestores the original OperationContext to the active context and recycles the OperationContextScope object.
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)

Use the OperationContextScope class to create a scope for a specific OperationContext object or a scope for a new OperationContext object using a specified IContextChannel object. An OperationContextScope can be used in a Windows Communication Foundation (WCF) service or WCF client application.

Once the OperationContextScope object has established the current operation context, you can use the OperationContext to:

  • Access and modify incoming and outgoing message headers and other properties.

  • Access the runtime, including dispatchers, the host, channel, and extensions.

  • Access other types of contexts, such as security, instance, and request contexts.

  • Access the channel associated with the OperationContext object or (if the channel implements System.ServiceModel.Channels.ISession) the associated channel's session identifier.

When an OperationContextScope is created, the current OperationContext is stored and the new OperationContext becomes the one returned by the Current property. When the OperationContextScope is disposed, the original OperationContext is restored.

Caution noteCaution

Do not use the asynchronous “await” pattern within a OperationContextScope block. When the continuation occurs, it may run on a different thread and OperationContextScope is thread specific. If you need to call “await” for an async call, use it outside of the OperationContextScope block.

The following example shows how to use the OperationContextScope to create a new context in a client application to add a custom header to the outgoing message.

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;

public class Client : ISampleServiceCallback
  ManualResetEvent wait = null;

    this.wait = new ManualResetEvent(false);

  public static void Main()
    Client client = new Client();

  void Run()
    // Picks up configuration from the config file.
    SampleServiceClient wcfClient = new SampleServiceClient(new InstanceContext(this));
      using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel))
        MessageHeader header
          = MessageHeader.CreateHeader(
          "Custom Happy Value."

        // Making calls.
        Console.WriteLine("Enter the greeting to send: ");
        string greeting = Console.ReadLine();

        header = MessageHeader.CreateHeader(
            "Different Happy Value."

        // One-way

        // Done with service. 
    catch (TimeoutException timeProblem)
      Console.WriteLine("The service operation timed out. " + timeProblem.Message);
    catch (CommunicationException commProblem)
      Console.WriteLine("There was a communication problem. " + commProblem.Message);

  #region ISampleServiceCallback Members

  public void PushBack(string msg)
    Console.WriteLine("Service said: " + msg);

  void WriteHeaders(MessageHeaders headers)
    Console.ForegroundColor = ConsoleColor.Red;
    Console.ForegroundColor = ConsoleColor.Blue;
    foreach (MessageHeaderInfo h in headers)
      if (!h.Actor.Equals(String.Empty))
        Console.WriteLine("\t" + h.Actor);
      Console.ForegroundColor = ConsoleColor.White;
      Console.WriteLine("\t" + h.Name);
      Console.ForegroundColor = ConsoleColor.Blue;
      Console.WriteLine("\t" + h.Namespace);
      Console.WriteLine("\t" + h.Relay);
      if (h.IsReferenceParameter == true)
        Console.WriteLine("IsReferenceParameter header detected: " + h.ToString());


.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

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