Finally I found a solution. (by reading Albahari C# in a NutShell)
First I defined a small assemby that both appdomains share.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
namespace TestAppDoms
{
public interface Ib2bBroker
{
void StartBroker();
}
public class b2bBrokerLink : MarshalByRefObject
{
public static Dictionary<string, Assembly> assemCache;
static Dictionary<string, byte[]> assemBinCache;
public b2bBrokerLink()
{
assemCache = new Dictionary<string, Assembly>();
}
public override object InitializeLifetimeService()
{
return null;
}
public void InitAssemCache(Dictionary<string, byte[]> _assemBins)
{
assemBinCache = _assemBins;
Console.WriteLine("Assems loaded");
}
public static Assembly brokerDom_AssemblyResolve(object sender, ResolveEventArgs args)
{
string shortName = new AssemblyName(args.Name).Name;
Console.WriteLine("Hello from brokerDom_AssemblyResolve");
if (assemCache.ContainsKey(shortName))
return assemCache[shortName];
if (assemBinCache.ContainsKey(shortName))
{
Assembly assem = Assembly.Load(assemBinCache[shortName]);
assemCache.Add(shortName,assem);
return assem;
}
return null;
}
}
}
Next modified BrokerCtrl.Run() as follows
Console.WriteLine("Hello From New Ctrl.dll " + AppDomain.CurrentDomain.FriendlyName);
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
AppDomain brokerDom = AppDomain.CreateDomain("b2bBroker", AppDomain.CurrentDomain.Evidence, setup);
brokerDom.AssemblyResolve += new ResolveEventHandler(b2bBrokerLink.brokerDom_AssemblyResolve);
b2bBrokerLink assemResolver = (b2bBrokerLink)brokerDom.CreateInstanceFromAndUnwrap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "b2bBrokerLink.dll"), "TestAppDoms.b2bBrokerLink");
Dictionary<string,> assemBinCache = new Dictionary<string,>();
using (FileStream fs = File.OpenRead(Path.Combine(@"C:\TestAssem\", "b2bBroker" + ".dll")))
{
byte[] assemBin = new byte[fs.Length];
fs.Read(assemBin, 0, Convert.ToInt32(fs.Length));
assemBinCache.Add("b2bBroker", assemBin);
}
assemResolver.InitAssemCache(assemBinCache);
Ib2bBroker broker = (Ib2bBroker)brokerDom.CreateInstanceAndUnwrap("b2bBroker", "TestAppDoms.b2bBroker");
broker.StartBroker();
AppDomain.Unload(brokerDom);
assemResolver = null;
Initiating b2bBrokerLink creates the new b2bBroker AppDomain and allows the main AppDomain download the most recent assembly-versions (as raw binary) and push assemCache (Direcrory<string,byte[]>) into the b2bBroker AppDomain.
Next start the a broker using the inteface Ib2bBroker and the brokerDom_AssemblyResolve will now be able to load the (most recent) assemblies into the new AppDomain.
It might be usefull for someone else - and maybe someone has a better solution.