Ok so... I have a WPF application (let's call it Launcher.exe
) which loads and executes another WPF application (let's call it Loaded.exe
) dynamically using something like this:
Byte[] assemblyData;
using (BinaryReader reader = new BinaryReader(new FileStream(filePath, FileMode.Open)))
assemblyData = reader.ReadBytes(Convert.ToInt32(fs.Length));
Assembly assembly = Assembly.Load(assemblyData);
MethodInfo method = assembly.EntryPoint;
if (method != null)
{
Object instance = assembly.CreateInstance(method.Name);
method.Invoke(o, null);
}
Now... the problem is that Launched.exe
has its own settings in the file Loaded.exe.config
, and it also uses them in bindings. For example:
Topmost="{Binding Mode=TwoWay, Path=Topmost, Source={x:Static p:Settings.Default}}"
First question is, how can I make my dynamically loaded assembly properly load/bind/update and, more generally, use its own settings? I don't think it can handle this automagically...
Second question is: can Loaded.exe
communicate with Launcher.exe
? Let's say Loaded.exe
needs some data that only Launcher.exe
can retrieve... how can it ask for it? I think I need something like a proxy between the two assemblies, but I can't even figure out how to start coding this...
I supose you'll need to load a separate assembly with it's own .config file, no? One way i do that is to load the assembly in a new AppDomain. You'll could deploy that assembly in a separate folder with all his needed references.
First setup the AppDomain, here you have a method:
Then get an instance of the object that executes the method on the assembly:
Even if we know the Object Type, we could create a method with generic type:
And then, invoke the method on the created instance, wich is executing in the new appDomain:
You could use like this:
For the second question, you could use NamedPipes, Remoting, WCF... You need to implement interprocess communication on the same machine. Take a look at MSDN documentaion, code sample covering this scenario with WCF http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx
See this sample on CodeProject, using Remoting Inter-process communication via Remoting