Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a method in a separate AppDomain?

Tags:

c#

.net

In my scenario, I want to execute a method in a separate AppDomain. How many different ways could I take to achieve this?

Especially, I have the following question:

  • Can I load an assembly into AppDomain A and execute its method in AppDomain B?
  • It seems I achieve this with AppDomain.DoCallBack Method and CrossAppDomainDelegate. I just cannot figure out how could it make sense to have the assembly and the method in different AppDomains. Is the same assmebly loaded again into the other AppDomain in order to execute the method?
like image 862
smwikipedia Avatar asked Aug 01 '10 03:08

smwikipedia


Video Answer


1 Answers

In order to execute code in a separate AppDomain, the assembly containing that code has to be loaded into that AppDomain. From there you can call a method in the second AppDomain from your hosting domain using reflection or the "CreateInstance" methods on the AppDomain class.

Keep in mind, AppDomains are hard boundaries. In order to communicate across the AppDomain boundaries, you will need to use remoting or a true IPC-ish mechanism.

This is a bit dated, but I believe it still applies. http://blogs.msdn.com/b/suzcook/archive/2003/06/12/57169.aspx

Suzanne Cook had a series of posts related to this subject so it may be worth browsing her archives a bit.

Check out the "CreateInstance" family of methods on the AppDomain class. In the past I've used CreateInstanceFromAndUnwrap() quite a bit for this type of situation.

As far as your question about how many ways there are to do this... Um, many, but it depends a lot on the exact situation and what objects you have in hand.

like image 85
Rob Cooke Avatar answered Sep 20 '22 02:09

Rob Cooke