FIX: CallContext objects are lost when you create a new serviced component

This article was previously published under Q319177
This article has been archived. It is offered "as is" and will no longer be updated.
If you set CallContext objects before you create an instance of a serviced component in a client application, you may lose the CallContext objects after you create the object instance.
This problem occurs if the objects that you place in the CallContext derive from the ILogicalThreadAffinative interface. If you place these objects in the CallContext (by using the CallContext.SetData method) and then create an instance of a ServicedComponent object, the CallContext.GetData method for those objects returns null.
To resolve this problem, obtain the latest service pack for Microsoft .NET Framework. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
318836 INFO: How to Obtain the Latest .NET Framework Service Pack


To work around this problem, use one of the following methods:
  • If you only need to use CallContext items locally in your application, do not derive these items from ILogicalThreadAffinative.

    NOTE: The remoting system does not pass these objects across AppDomains.
  • If you want to use remoting servers with ServicedComponent objects, and if you want to flow CallContext to them, you must create an instance of your ServicedComponent objects before you place those items in the CallContext.

    If you cannot control this, you can store the contents of the CallContext before you create an instance of your ServicedComponent and then load the contents back in.
NOTE: You cannot take advantage of the CallContext feature in ServicedComponent applications. Microsoft does not support the use of CallContext from within ServicedComponent objects.
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. This problem was first corrected in Microsoft .NET Framework Service Pack 2 (SP2).
With the CallContext object, you can send information along the channel from client to server or vice versa. CallContext provides a set of properties that are carried along with the execution code path (call chain).

This problem also occurs with all headers in the CallContext object.

Steps to Reproduce the Problem

  1. Define instances of a class to place in the CallContext:
    [Serializable]public class CtxObject : ILogicalThreadAffinative{	public string CtxString;	public CtxObject(){CtxString = "Some String" ;}}					
  2. Place one of these objects in a CallContext data slot, and then create an instance of a ServicedComponent:
    // Create CallContext object.CtxObject CtxObj = new CtxObject () ;                     // Set it in the CallContext.CallContext.SetData ("CtxObject", CtxObj) ;// Create the serviced component.MyServicedComponent MyObj = new MyServicedComponent () ;  // Retrieve the CallContext object (returns null).CtxObject CtxObj1 = (CtxObject) CallContext.GetData ("CtxObject") ;  					
ERROR: PhantomJS timeout occurred