This documentation is archived and is not being maintained.

OperationContextScope Class

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


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

Public NotInheritable Class OperationContextScope _
	Implements 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.)
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 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 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 service or 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.

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.

Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading

Public Class Client
	Implements ISampleServiceCallback
  Private wait As ManualResetEvent = Nothing

  Private Sub New()
	Me.wait = New ManualResetEvent(False)
  End Sub

  Public Shared Sub Main()
	Dim client As New Client()
  End Sub

  Private Sub Run()
	' Picks up configuration from the config file.
	Dim wcfClient As New SampleServiceClient(New InstanceContext(Me))
	  Using scope As New OperationContextScope(wcfClient.InnerChannel)
                Dim header As MessageHeader = MessageHeader.CreateHeader("Service-Bound-CustomHeader", _
                                    "http://Microsoft.WCF.Documentation", "Custom Happy Value.")

		' Making calls.
		Console.WriteLine("Enter the greeting to send: ")
		Dim greeting As String = Console.ReadLine()

                header = MessageHeader.CreateHeader("Service-Bound-OneWayHeader", _
                                                    "http://Microsoft.WCF.Documentation", "Different Happy Value.")

		' One-way

		' Done with service. 
	  End Using
	Catch timeProblem As TimeoutException
	  Console.WriteLine("The service operation timed out. " & timeProblem.Message)
	Catch commProblem As CommunicationException
	  Console.WriteLine("There was a communication problem. " & commProblem.Message)
	End Try
  End Sub

  #Region "ISampleServiceCallback Members"

  Public Sub PushBack(ByVal msg As String) Implements ISampleServiceCallback.PushBack
	Console.WriteLine("Service said: " & msg)
  End Sub

  Private Sub WriteHeaders(ByVal headers As MessageHeaders)
	Console.ForegroundColor = ConsoleColor.Red
	Console.ForegroundColor = ConsoleColor.Blue
	For Each h As MessageHeaderInfo In headers
	  If Not h.Actor.Equals(String.Empty) Then
		Console.WriteLine(vbTab & h.Actor)
	  End If
	  Console.ForegroundColor = ConsoleColor.White
	  Console.WriteLine(vbTab & h.Name)
	  Console.ForegroundColor = ConsoleColor.Blue
	  Console.WriteLine(vbTab & h.Namespace)
	  Console.WriteLine(vbTab & h.Relay)
	  If h.IsReferenceParameter = True Then
		Console.WriteLine("IsReferenceParameter header detected: " & h.ToString())
	  End If
	Next h
  End Sub
  #End Region

End Class

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