Export (0) Print
Expand All

OperationContextScope Class

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

System.Object
  System.ServiceModel.OperationContextScope

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

'Declaration
Public NotInheritable Class OperationContextScope _
	Implements IDisposable

The OperationContextScope type exposes the following members.

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

  NameDescription
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.)
Top

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.

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()
	client.Run()
  End Sub 

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

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

		'Console.ReadLine();
                header = MessageHeader.CreateHeader("Service-Bound-OneWayHeader", _
                                                    "http://Microsoft.WCF.Documentation", "Different Happy Value.")
		OperationContext.Current.OutgoingMessageHeaders.Add(header)

		' One-way
		wcfClient.Push(greeting)
		Me.wait.WaitOne()

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


  #Region "ISampleServiceCallback Members" 

  Public Sub PushBack(ByVal msg As String) Implements ISampleServiceCallback.PushBack
	Console.WriteLine("Service said: " & msg)
	Me.WriteHeaders(OperationContext.Current.IncomingMessageHeaders)
	Me.wait.Set()
  End Sub 

  Private Sub WriteHeaders(ByVal headers As MessageHeaders)
	Console.ForegroundColor = ConsoleColor.Red
	Console.WriteLine("IncomingHeader:")
	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
	Console.ResetColor()
  End Sub
  #End Region

End Class

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

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