0

How to SetData to new created AppDomain. When i DoCallBack to my testFunc i receive "System.NullReferenceException" exception. What i do wrong?

var client = "test";
var engine = 123;

AppDomain appDomain = AppDomain.CreateDomain("newDomain");

appDomain.SetData("client", client);
appDomain.SetData("engine", engine);

appDomain.DoCallBack(testFunc);

 private void testFunc()
 {
    var client = (string)AppDomain.CurrentDomain.GetData("client");
    var engine = (int)AppDomain.CurrentDomain.GetData("engine");

    Console.WriteLine("client: " + client);
    Console.WriteLine("engine: " + engine);
 }

Setting vars globaly for AppDomain don't change anathing, same error.

AppDomain.CurrentDomain.SetData("client", client);
AppDomain.CurrentDomain.SetData("engine", engine);

P.S. I receive System.NullReferenceException, because AppDomain can't find that vars that i was setup before DoCallBack. So how to setup them in right way?

SLI
  • 713
  • 11
  • 29

1 Answers1

0

If you show us the correct code then you can't set non-static and without instance method to DoCallBack().

The method should be a static:

private static void testFunc()
 {
    var client = (string)AppDomain.CurrentDomain.GetData("client");
    var engine = (int)AppDomain.CurrentDomain.GetData("engine");

    Console.WriteLine("client: " + client);
    Console.WriteLine("engine: " + engine);
 }

or you have to create a instance before pass into DoCallBack()

var instance = new Program();
appDomain.DoCallBack(instance.testFunc);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116