System.Runtime.Remoting.Messaging.CallContext Class

Assembly: Mscorlib.dll
Namespace: System.Runtime.Remoting.Messaging
Summary
Provides a set of properties that are carried with the execution code path. This class cannot be inherited.
C# Syntax:
[Serializable]
public sealed class CallContext
Remarks
CallContext is a specialized collection object similar to a Thread Local Storage for method calls, and provides data slots that are unique to each logical thread of execution. The slots are not shared across call contexts on other logical threads. Objects can be added to the CallContext as it travels down and back up the execution code path, and examined by various objects along the path.

When a remote method call is made to an object in another AppDomain, the CallContext class generates a LogicalCallContext instance that travels along with the remote call. Only objects that expose the ILogicalThreadAffinative interface and are stored in the CallContext are propagated outside the AppDomain in a LogicalCallContext. Objects that do not support this interface are not transmitted in LogicalCallContext instances with remote method calls.



Note All methods in CallContext are static and operate on the call context in the current Thread.
Example
The following code example demonstrates the use of the CallContext class to transmit the conceptual topic at MSDN: principalidentityobjects to a remote location for identification. To view the code for the LogicalCallContextData class used in this sample, see the example for the ILogicalThreadAffinative interface. To view the code for the HelloServiceClass class used in this sample, see the example for the CallContext.GetData method. To view the code for the server class used in this sample, see example for the RemotingConfiguration.RegisterActivatedServiceType class.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;

public class ClientClass {

   public static void Main() {
      
      GenericIdentity ident = new GenericIdentity("Bob");
      GenericPrincipal prpal = new GenericPrincipal(ident, 
                                                    new string[] {"Level1"});
      LogicalCallContextData data = new LogicalCallContextData(prpal);      
      
      //Enter data into the CallContext
      CallContext.SetData("test data", data);

      
      Console.WriteLine(data.numOfAccesses);

      ChannelServices.RegisterChannel(new TcpChannel());

      RemotingConfiguration.RegisterActivatedClientType(typeof(HelloServiceClass),
                                                        "tcp://localhost:8082");

      HelloServiceClass service = new HelloServiceClass();

      if(service == null) {
          Console.WriteLine("Could not locate server.");
          return;
      }


      // call remote method
      Console.WriteLine();
      Console.WriteLine("Calling remote object");
      Console.WriteLine(service.HelloMethod("Caveman"));
      Console.WriteLine(service.HelloMethod("Spaceman"));
      Console.WriteLine(service.HelloMethod("Bob"));
      Console.WriteLine("Finished remote object call");
      Console.WriteLine();

      //Extract the returned data from the call context
      LogicalCallContextData returnedData = 
         (LogicalCallContextData)CallContext.GetData("test data");

      Console.WriteLine(data.numOfAccesses);
      Console.WriteLine(returnedData.numOfAccesses);
   }
}

    
See also:
System.Runtime.Remoting.Messaging Namespace

System.Runtime.Remoting.Messaging.CallContext Member List:

Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
FreeNamedDataSlot Empties a data slot with the specified name.
GetData Retrieves an object with the specified name from the CallContext.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetHeaders Returns the headers that are sent along with the method call.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
SetData Stores a given object and associates it with the specified name.
SetHeaders Sets the headers that are sent along with the method call.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Runtime.Remoting.Messaging.CallContext Member Details

Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~CallContext();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: FreeNamedDataSlot(
   string name
)
Summary
Empties a data slot with the specified name.
C# Syntax:
public static void FreeNamedDataSlot(
   string name
);
Parameters:

name

The name of the data slot to empty.

Return to top


Method: GetData(
   string name
)
Summary
Retrieves an object with the specified name from the CallContext.
C# Syntax:
public static object GetData(
   string name
);
Parameters:

name

The name of the item in the call context.

Return Value:
The object in the call context associated with the specified name.
Example
The following code example demonstrates the use of the CallContext.GetData method to transmit the conceptual topic at MSDN: principalidentityobjects to a remote location for identification. To view the code for the LogicalCallContextData class used in this sample, see the example for the ILogicalThreadAffinative interface. To view the code for the client class used in the sample, see the example for the CallContext class. To view the code for the server class used in this sample, see example for the RemotingConfiguration.RegisterActivatedServiceType class.
using System;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;

public class HelloServiceClass : MarshalByRefObject {

   static int n_instances;
   int instanceNum;

   public HelloServiceClass() {
      n_instances++;
      instanceNum = n_instances;
      Console.WriteLine(this.GetType().Name + " has been created.  Instance # = {0}", instanceNum);
   }


   ~HelloServiceClass() {
      Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", instanceNum);      
   }


   public String HelloMethod(String name) {

      //Extract the call context data
      LogicalCallContextData data = (LogicalCallContextData)CallContext.GetData("test data");      
      IPrincipal myPrincipal = data.Principal;
      
      //Check the user identity
      if(myPrincipal.Identity.Name == "Bob") {
         Console.WriteLine("\nHello {0}, you are identified!", myPrincipal.Identity.Name);
         Console.WriteLine(data.numOfAccesses);
      }
      else {
         Console.WriteLine("Go away! You are not identified!");
         return String.Empty;
      }

        // calculate and return result to client	
      return "Hi there " + name + ".";
   }
}

    

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHeaders()
Summary
Returns the headers that are sent along with the method call.
C# Syntax:
public static Header[] GetHeaders();
Return Value:
The headers that are sent along with the method call.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: SetData(
   string name,
   object data
)
Summary
Stores a given object and associates it with the specified name.
C# Syntax:
public static void SetData(
   string name,
   object data
);
Parameters:

name

The name with which to associate the new item in the call context.

data

The object to store in the call context.

Example
The following code example demonstrates the use of the CallContext.SetData method to transmit the conceptual topic at MSDN: principalidentityobjects to a remote location for identification. To view the code for the LogicalCallContextData class used in this sample, see the example for the ILogicalThreadAffinative interface. To view the code for the HelloServiceClass class used in this sample, see the example for the CallContext.GetData method. To view the code for the server class used in this sample, see example for the RemotingConfiguration.RegisterActivatedServiceType class.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Security.Principal;

public class ClientClass {

   public static void Main() {
      
      GenericIdentity ident = new GenericIdentity("Bob");
      GenericPrincipal prpal = new GenericPrincipal(ident, 
                                                    new string[] {"Level1"});
      LogicalCallContextData data = new LogicalCallContextData(prpal);      
      
      //Enter data into the CallContext
      CallContext.SetData("test data", data);

      
      Console.WriteLine(data.numOfAccesses);

      ChannelServices.RegisterChannel(new TcpChannel());

      RemotingConfiguration.RegisterActivatedClientType(typeof(HelloServiceClass),
                                                        "tcp://localhost:8082");

      HelloServiceClass service = new HelloServiceClass();

      if(service == null) {
          Console.WriteLine("Could not locate server.");
          return;
      }


      // call remote method
      Console.WriteLine();
      Console.WriteLine("Calling remote object");
      Console.WriteLine(service.HelloMethod("Caveman"));
      Console.WriteLine(service.HelloMethod("Spaceman"));
      Console.WriteLine(service.HelloMethod("Bob"));
      Console.WriteLine("Finished remote object call");
      Console.WriteLine();

      //Extract the returned data from the call context
      LogicalCallContextData returnedData = 
         (LogicalCallContextData)CallContext.GetData("test data");

      Console.WriteLine(data.numOfAccesses);
      Console.WriteLine(returnedData.numOfAccesses);
   }
}

    

Return to top


Method: SetHeaders(
   Header[] headers
)
Summary
Sets the headers that are sent along with the method call.
C# Syntax:
public static void SetHeaders(
   Header[] headers
);
Parameters:

headers

An Header array of the headers that are to be sent along with the method call.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.