12

I need to call a method of object in another appdomain (pass param and get result). Ideas?

UPD both AppDomain's are created not by my code (host app creates it, and then my code gets called). How I can get access to one AppDomain from another?

user626528
  • 13,999
  • 30
  • 78
  • 146

2 Answers2

14

If you created an object in another domain e.g. with AppDomain.CreateInstanceAndUnwrap, all you need to call the object in another domain is to call an object's method.

The simplest way to make a cross-application domain call is just to make a call directly on that object, which actually is exposed from another domain via its proxy, existing in another domain.

UPD
Unfortunately getting the host domain is not that easy. You should enumerate domains like this and find among them the host one. I suppose that your host domain is the one for which the method AppDomain.IsDefaultAppDomain returns true.

Community
  • 1
  • 1
Centro
  • 3,892
  • 2
  • 25
  • 31
  • I complete forgot about that method. Actually quite more convenient than using DoCallBack ;) – Oliver Hanappi Jun 05 '11 at 12:42
  • Nice! Is it possible to make callbacks using this way? – user626528 Jun 05 '11 at 13:08
  • 2
    @user626528 Yes, but not just a callback for one method of an object. If you create an object in another domain, all its methods are called in that domain. I suppose that you should create some class that will be responsible for your callback, then create an instance of that class in another domain via `AppDomain.CreateInstanceAndUnwrap` and, finally, call the method on the instance in the other domain. – Centro Jun 05 '11 at 13:25
  • @user626528 Please see the UPD. – Centro Jun 05 '11 at 13:57
3

This is usually achieved using AppDomain.DoCallBack. You need to make sure that if you want to pass parameters, you need to create a serializable object, whose method you pass to the method described above. In the callback method you can perform another AppDomain callback to pass the result back to the original AppDomain.

hoang
  • 1,887
  • 1
  • 24
  • 34
Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68
  • I don't see, how to pass parameter/result with it. – user626528 Jun 05 '11 at 11:41
  • 1
    @user626528 You cannot pass parameter or get result directly. What you can do is you can use [SetData](http://msdn.microsoft.com/en-us/library/system.appdomain.setdata.aspx) and [GetData](http://msdn.microsoft.com/en-us/library/system.appdomain.getdata.aspx) method of your app domain to do so. – Prasad Feb 04 '13 at 11:28