How to handle data exchange between cross-application domains

We create a .NET application where we load external code assemblies ("plugins"). Until that moment, we downloaded these assemblies into one (main) application domain.

We would like to be able to unload the assembly after loading it.

To this end, we are developing a system that will create a separate additional AppDomain to host plugin builders that will be unloaded at will.

The problems we have with this approach are:

  • The plug-in DLL module should interact with classes in the main AppDomain (for example, logger).
  • Data that is sent to the plugin dll is not necessarily marked as Serializable or retrieved from MarshalByRefObj.

Is there any common practice of splitting an application in such cases? What is the best decision we could take?

Another interesting question: why does MarshalByRef not follow the attribute and force us to deduce from the object?

+5
source share
1 answer

The easiest way to communicate through AppDomains is to use AppDomain.CreateInstanceAndUnwrap . This will allow you to instantiate the object in another AppDomain and return the proxy server to this object. From a proxy server, you can effectively make method calls in the application domain.

If your current classes do not extend MarshalByRef, then you should create a bridge class that does and can act as an intermediate.

+3
source

All Articles