Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 .

C#
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?
C#
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 18:05pm
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900