Export (0) Print
Expand All

OperationContext Class

Provides access to the execution context of a service method.

System.Object
  System.ServiceModel.OperationContext

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

public sealed class OperationContext : IExtensibleObject<OperationContext>

The OperationContext type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsOperationContextInitializes a new instance of the OperationContext class that uses the specified IContextChannel in a client application.
Top

  NameDescription
Public propertyChannelGets the channel associated with the current OperationContext object.
Public propertyClaimsPrincipalGets the claims-based principal associated with the operation.
Public propertyStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCurrentGets or sets the execution context for the current thread.
Public propertyEndpointDispatcherGets or sets the endpoint dispatcher for the endpoint to inspect or modify.
Public propertySupported in .NET for Windows Store appsExtensionsGets the collection of service extensions from the current message context.
Public propertyHasSupportingTokensGets a value that indicates whether the incoming message has supporting tokens.
Public propertyHostGets the ServiceHost for the current service object.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIncomingMessageHeadersGets the incoming message headers for the OperationContext.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIncomingMessagePropertiesGets the message properties for the incoming message in the OperationContext.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIncomingMessageVersionGets the incoming SOAP message version for the OperationContext.
Public propertyInstanceContextGets the InstanceContext object that manages the current service instance.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsUserContextThis property is intended for system use and should not be called by users.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsOutgoingMessageHeadersGets the outgoing message headers for the active OperationContext.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsOutgoingMessagePropertiesGets the message properties for the outbound message in the active OperationContext.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsRequestContextGets or sets the RequestContext implementation for this method.
Public propertyServiceSecurityContextGets or sets the ServiceSecurityContext within which this method executes.
Public propertySessionIdGets the String used to identify the current session.
Public propertySupportingTokensGets a ICollection<T> of type System.IdentityModel.Tokens.SecurityToken.
Top

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetCallbackChannel<T>Gets a channel to the client instance that called the current operation.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSetTransactionCompleteCommits the currently executing transaction.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventSupported by Portable Class LibrarySupported in .NET for Windows Store appsOperationCompletedOccurs 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.

NoteNote

For an example that uses the OperationContext class in a client scenario, see OperationContextScope.

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

namespace Microsoft.WCF.Documentation
{
  public class Client : SampleDuplexHelloCallback
  {
    AutoResetEvent waitHandle;

    public Client()
    {
      waitHandle = new AutoResetEvent(false);
    }

    public void Run()
    {
      // Picks up configuration from the config file.
      SampleDuplexHelloClient wcfClient 
        = new SampleDuplexHelloClient(new InstanceContext(this));
      try
      {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Enter a greeting to send and press ENTER: ");
        Console.Write(">>> ");
        Console.ForegroundColor = ConsoleColor.Green;
        string greeting = Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Called service with: \r\n\t" + greeting);
        wcfClient.Hello(greeting);
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
        this.waitHandle.WaitOne();
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Set was called.");
        Console.Write("Press ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("ENTER");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write(" to exit...");
        Console.ReadLine();
        wcfClient.Close();
      }
      catch (TimeoutException timeProblem)
      {
        Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        Console.ReadLine();
        wcfClient.Abort();
      }
      catch (CommunicationException commProblem)
      {
        Console.WriteLine("There was a communication problem. " + commProblem.Message);
        Console.ReadLine();
        wcfClient.Abort();
      }
    }

    public static void Main()
    {
      Client client = new Client();
      client.Run();
    }

    public void Reply(string response)
    {
      Console.WriteLine("Received output.");
      Console.WriteLine("\r\n\t" + response);
      this.waitHandle.Set();
    }
  }
}

.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

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft