Click here to Skip to main content
14,429,861 members
Rate this:
Please Sign up or sign in to vote.
Hi i stored class object in logical data(call context) ,when i try to access the logical data(call context) from different app domain,I'm getting system.runtime.serialization issue.

Following my code will reproduce my issue .

public static class Program
  {
      private const String c_CCDataName = "CCData";

      private static void Main()
      {
          // Set an item in the thread's call context
          CallContext.LogicalSetData(c_CCDataName,new MyClass());

          // Get the item in the thread's call context
          GetCallContext();

          // Show that call context flows to another thread
          WaitCallback wc = na => GetCallContext();
          wc.EndInvoke(wc.BeginInvoke(null, null, null));

          // Show that call context flows to another AppDomain
          AppDomain ad = AppDomain.CreateDomain("Other AppDomain");
          ad.DoCallBack(GetCallContext);
          AppDomain.Unload(ad);

          // Remove the key to prevent (de)serialization of its value
          // from this point on improving performance
          ///CallContext.FreeNamedDataSlot(c_CCDataName);

          // Show no data due to the key being removed from the hashtable
          GetCallContext();

          Console.ReadLine();
      }

      private static void GetCallContext()
      {
          // Get the item in the thread's call context
          Console.WriteLine("AppDomain={0}, Thread ID={1}, Data={2}",
             AppDomain.CurrentDomain.FriendlyName,
             Thread.CurrentThread.ManagedThreadId,
             CallContext.LogicalGetData(c_CCDataName));
      }

      class MyClass
      {
          public int id { get; set; }
      }

  }


Following code will fix the issue.But i'm not sure its best solution or not.Could you suggest me?
public static class Program
{
    private const String c_CCDataName = "CCData";

    private static void Main()
    {
        // Set an item in the thread's call context
        CallContext.LogicalSetData(c_CCDataName,new MyClass());

        // Get the item in the thread's call context
        GetCallContext();

        // Show that call context flows to another thread
        WaitCallback wc = na => GetCallContext();
        wc.EndInvoke(wc.BeginInvoke(null, null, null));

        // Show that call context flows to another AppDomain
        AppDomain ad = AppDomain.CreateDomain("Other AppDomain");
        ad.DoCallBack(GetCallContext);
        AppDomain.Unload(ad);

        // Remove the key to prevent (de)serialization of its value
        // from this point on improving performance
        ///CallContext.FreeNamedDataSlot(c_CCDataName);

        // Show no data due to the key being removed from the hashtable
        GetCallContext();

        Console.ReadLine();
    }

    private static void GetCallContext()
    {
        // Get the item in the thread's call context
        Console.WriteLine("AppDomain={0}, Thread ID={1}, Data={2}",
           AppDomain.CurrentDomain.FriendlyName,
           Thread.CurrentThread.ManagedThreadId,
           CallContext.LogicalGetData(c_CCDataName));
    }

    class MyClass:MarshalByRefObject
    {
        public int id { get; set; }
    }

}


i just used
MarshalByRefObject
it 's solved my issue.could you suggest me it's any impact will be applicable ,in case of using
<pre>MarshalByRefObject</pre>
.

thanks in advance
mani
Posted
Updated 14-Dec-13 19:05pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100