This article briefly describes how to increase the speed while marshaling across application domains. The intention of this how-to is to provide some tips and some clues about this issue.
Fast cross-AppDomain calls were a need for me, while trying to implement a plug-in architecture for my application. The one problem of performance I've faced was that cross application domain method calls were 1000x slower than standard method calls.
Using the Code
The solution has three projects: one for the application executable, another one for the "
PluginInterface", and another for the plugin itself. Both projects, the plugin and the executable reference the
PluginInterface. That's because when executing your program, referenced libraries get loaded automatically in the default
AppDomain, and then, it's impossible to unload them. By creating an intermediate DLL (the
PluginProxy) we can create an
AppDomain in which types loaded by this DLL remain isolated from the default
AppDomain, giving us the possibility to unload previously loaded plugins, using
To describe how to improve this situation, let's define some classes to use as an example.
The plugin class. This class should be in an independent assembly, like "Plugin.dll". The catch: it is ~1000x slower to invoke a method for an object instance residing in another application domain.
public class Plugin : MarshalByRefObject, PluginInterface.IPlugin
public string SendMessage(string message)
return "Received: " + message;
IPluginProxy and the
IPlugin interfaces should also be in another independent assembly, like "PluginInterface.dll". For the sake of simplicity of this article,
IPluginProxy's implementation is left as an exercise to the reader.
public interface IPlugin
string SendMessage(string message);
public interface IPluginProxy
IPlugin Create(AssemblyName assemblyName);
Last but not least, the plug-in consumer, your executable file. The first tip for speeding up marshaling is right here, in your application's entry point.
static void Main()
AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain");
PluginInterface.IPlugin plugin = proxy.Create(pluginAssembly);
plugin.SendMessage("hello from another AppDomain");
Now, for this to work faster, first it's necessary to:
- Apply a strong name to PluginInterface.dll and to the plugin(s), in this example, "Plugin.dll".
- Install PluginInterface.dll and Plugin.dll into the GAC.
- The entry point of your application should have the following attribute applied:
And... that's it. If you followed those steps, and your plugin is getting loaded in another domain, other than the default domain, then, you should see some improvements, like this:
|Standard Method Calls/sec ||
|Cross AppDomain Method Calls/sec||
- 24th April, 2009: Initial post