switch appdomain inside a callback method RRS feed

  • Question

  • Hello we have the following code

    string MyCallbackCore(string instring)
    	string result = string.Empty;
    	// code to get result using instring
    	return result
    // this method could be called inside apddomain with id 1
    string MyCallback(string instring)
    	if (AppDomain.CurrentDomain.Id != 2)
    		string transferedResult = string.Empty;
    		// <--- Begin Don't know 
    		switch to appdomain 2 and transfer instring to appdomain 2
    		call MyCallbackCore with transfered instring
    		switch to appdomain 1 and transfer result of MyCallbackCore to transferedResult;
    		// <--- End Don't know 
    		return transferedResult;
    		return MyCallbackCore(string instring)

    We have  Situation where MyCallback is called form a method which is in inside appdomain 1,

    so we have to switch to appdomain 2 before calling MyCallbackCore. (See the comments in the if banch).

    Can somebody gives some hints what to use inside the if branch ?


      Hendrik Schmieder

    Wednesday, August 28, 2019 1:16 PM

All replies

  • Hi h_schmieder, 

    Thank you for posting here.

    For your question, you want to share data between different AppDomains.

    I make a test on my side, and you can refer the following code.

            static void MyCallback()
                // Gets the value stored in the current application domain.
                string str = AppDomain.CurrentDomain.GetData("data") as string;
                str = str.Insert(0, "lowercase:");
                Console.WriteLine($"AppDomain{AppDomain.CurrentDomain.Id}: {str}");
                AppDomain.CurrentDomain.SetData("data", str);
            static string MyCallbackCore(string instring)
                instring = instring.ToLower();
                Console.WriteLine($"AppDomain{AppDomain.CurrentDomain.Id}: {instring}");
                // Create the second AppDomain.
                AppDomain domain = AppDomain.CreateDomain("Test");
                domain.SetData("data", instring);
                // Executes the code in another application domain.
                string str = domain.GetData("data") as string;
                str += ",end"; 
                Console.WriteLine($"AppDomain{AppDomain.CurrentDomain.Id}: {str}");
                return str;
            static void Main(string[] args)
                string str = MyCallbackCore("Hello");


    Hope it can help you.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, August 29, 2019 8:30 AM