Sharing Data Between Appdomains

Sharing data between AppDomains

The only way to avoid serialisation is to represent your data using objects which derive from MarshalByRefObject, but in that case you will still have the cost of marshalling across the AppDomain boundaries. This may also involve the refactoring/re-writing of much of your code.

Assuming marshalling by reference is not an option, you will have to serialise at some point. It simply cannot be avoided. One way to do this is as Neil Barnwell suggests, with a database, another would be with a local file as you suggest yourself.

Another way which may or may not feasible depending on your delivery timeline and/or .NET 4.0 adoption, would be to use a memory mapped file, see .Net Framework 4.0: Using memory mapped files.

How can I share the data of a dictionary between two appdomains in the same process

You cannot share an object between two appdomains. Have a look at .net Remoting or WCF if you need to share data between two or more appdomains.

Sharing data between AppDomain using GAC (System.InvalidCastException)

My guess is that you did not disable the Visual Studio Hosting process in the Project Debug Settings. Visual Studio will host the code in the hosting process within an AppDomain with no assembly sharing.

Communication between AppDomains

Unless you specifically want to avoid WCF for some reason, I would suggest taking a look at it. Specifically, you can use the NetNamedPipeBinding, which provides for communication on the same machine using named pipes. You can find some more information here:
http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx

As well, here is a reasonably concise blog entry demonstrating it's use (from a WMP plug-in to a third-party app).

Based on your description of the application, you could establish a WCF service in the first AppDomain then call into that service from the second AppDomain.

How best to communicate between AppDomains?

I have had good success using WCF with a named pipes binding. Using named pipes creates no network traffic and uses binary encoding, so it should be pretty fast without sacrificing the ability to distribute in future scaling scenarios.

EDIT:
Refer here for more detailed information including a link to an implementation example.

How to pass a variable from one app domain to another

Use one of the variations of AppDomain.CreateDomain that takes an AppDomainSetup argument. In the AppDomainSetup object, set the AppDomainInitializerArguments member to the string array you want passed to the new app domain.

See sample code at http://msdn.microsoft.com/en-us/library/system.appdomainsetup.appdomaininitializerarguments.aspx.

With the code in the question, the change might look like (not tested):

static voide Main(string[] args) {
_str = "abc";

AppDomainSetup setup = new AppDomainSetup();
setup.AppDomainInitializer = new AppDomainInitializer(MyNewAppDomainMethod);
setup.AppDomainInitializerArguments = new string[] { _str };

AppDomain domain = AppDomain.CreateDomain(
"Domain666",
new Evidence(AppDomain.CurrentDomain.Evidence),
setup);

Console.WriteLine("Finished");
Console.ReadKey();
}

static void MyNewAppDomainMethod(string[] args) {
...
}


Related Topics



Leave a reply



Submit