Click here to Skip to main content
12,999,628 members (53,285 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 24 Apr 2009

Fast Marshaling for Cross-AppDomain Proxies

, 24 Apr 2009
Rate this:
Please Sign up or sign in to vote.
This article describes how to speed up marshaling across application domains.


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 AppDomain.Unload(...).

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;

Then the 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.

[STAThread, LoaderOptimization(LoaderOptimization.MultiDomainHost)]
static void Main()
    AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain");

    [...]  // you should create an instance of the proxy in the pluginDomain AppDomain.
    [...]  // Then, by calling proxy.Create() the proxy should create an instance
    [...]  // of the plug-in in that AppDomain, so it can be unloaded later if necessary.

    PluginInterface.IPlugin plugin = proxy.Create(pluginAssembly);

    plugin.SendMessage("hello from another AppDomain");  // if all goes well, 
					// this call should be 10x faster

Now, for this to work faster, first it's necessary to:  

  1. Apply a strong name to PluginInterface.dll and to the plugin(s), in this example, "Plugin.dll".
  2. Install PluginInterface.dll and Plugin.dll into the GAC.
  3. 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 Implementation

Optimized Implementation
Standard Method Calls/sec 

Average: ~11600000 

Cross AppDomain Method Calls/sec




  • 24th April, 2009: Initial post 


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Argentina Argentina
No Biography provided

You may also be interested in...

Comments and Discussions

Question[My vote of 1] full source code sample ? Pin
kiquenet.com3-Dec-15 23:09
memberkiquenet.com3-Dec-15 23:09 
QuestionWhy not System.AddIn Pin
Ralph Shillington18-Jul-13 2:04
memberRalph Shillington18-Jul-13 2:04 
GeneralSource code for IPluginProxy would be very helpful Pin
bscaer28-Apr-09 4:58
memberbscaer28-Apr-09 4:58 
GeneralRe: Source code for IPluginProxy would be very helpful Pin
reflex@codeproject1-May-09 8:36
memberreflex@codeproject1-May-09 8:36 
GeneralMy vote of 5 Pin
ADLER125-Apr-09 5:59
memberADLER125-Apr-09 5:59 
GeneralRe: My vote of 5 Pin
reflex@codeproject26-Apr-09 9:16
memberreflex@codeproject26-Apr-09 9:16 
GeneralRe: My vote of 5 Pin
trevorde stickman29-Apr-09 10:42
membertrevorde stickman29-Apr-09 10:42 
GeneralRe: My vote of 5 Pin
reflex@codeproject1-May-09 8:42
memberreflex@codeproject1-May-09 8:42 
Yes i know the lack of code is a problem, but the reason for me not to post the code is because it's part of an enterprise project (closed source)...

... and the reason for them to be in the GAC is: the internal logic of the framework for "Fast Path" marshaling does not get activated if the assemblies are not installed in the GAC in first place. (Although i don't know why this is a requirement)

Good luck Thumbs Up | :thumbsup: ,

GeneralRe: My vote of 5 Pin
tgrk18-Dec-09 7:05
membertgrk18-Dec-09 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 24 Apr 2009
Article Copyright 2009 by reflex@codeproject
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid