Partager via


Using CallContext

The call context provides a set of properties that are carried with the execution code path. Entries can be added to the instance of the CallContext class as it travels down and back up the execution code path.

The CallContext object is passed in the IMessage implementation in the "__CallContext" entry (of type LogicalCallContext) as it flows between contexts and application domains. Message sinks can add and look up entries in the __CallContext as it passes through the message sink chain.

The call context is cloned at the application domain boundary. Objects that expose the ILogicalThreadAffinative interface are propagated outside the application domain and added to the thread of the receiver. Objects that do not support this interface are not propagated outside the application domain.

The CallContext provides data slots for the call path. CallContext.SetData and CallContext.GetData can be used to manage the call context slots in application code. The data slots are unique per call path; that is, the state is not shared across call paths. The slots are named. The name is used to access the data slot. The slot can be explicitly freed using the name.

For a remoting example using CallContext, see Remoting Example: CallContext.

Async and CallContext

The BeginInvoke method passes the CallContext to the server. When EndInvoke method is called, the CallContext is merged back onto the thread. This includes cases where BeginInvoke and EndInvoke are called sequentially and where BeginInvoke is called on one thread and EndInvoke is called on a callback function.

When CallContext entries are common to both the CallContext on the current thread and the CallContext provided by EndInvoke, the entries from the EndInvoke overwrite the entries in the CallContext on the thread.

Context-Relative Static Fields

Context-relative static fields provide fields that are relative to the context. Whenever the static field is accessed from a particular context, the value will be relative to that context; the field is not shared between contexts. If the static field is accessed in another context, the field will contain a different value.

To use context-relative static fields, simply apply the ContextStaticAttribute attribute to a field on a ContextBoundObject object.

See Also

CallContexts and Direct Remoting | Advanced Remoting | Remoting Example: CallContext | CallContext