This documentation is archived and is not being maintained.

OperationContextScope Constructor (IContextChannel)

Initializes a new instance of the OperationContextScope class that uses the specified IContextChannel to create a new OperationContext for the scope.

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

public OperationContextScope(
	IContextChannel channel


Type: System.ServiceModel.IContextChannel
The channel to use when creating the scope for a new OperationContext.

Use the OperationContextScope constructor to create a new OperationContext for a client channel that can be used to add or modify outbound message headers, read incoming message headers, or access other run-time properties of the OperationContext.

The headers added to the OperationContext.IncomingMessageHeaders property of the newly created OperationContext apply only to the channel that was passed to the OperationContextScope constructor. If the user creates a new channel within its scope then the headers are not applied to messages sent on the new channel.

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

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);

.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.