Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This was asked on the stackoverflow forums a while back, but was unanswered. Does anyone have a solution?

If the asm variable is a MemoryStream and contains a .Net assembly, you would normally run it like this:

var asm   = Assembly.Load(ms.ToArray());
var entry = asm.EntryPoint;
var inst  = asm.CreateInstance(entry.Name);
entry.Invoke(inst, null);


This works well on console applications and windows forms applications, however, WPF applications throw an exception:
Exception has been thrown by the target of an invocation.


With the inner exception of type System.IO.IOException:
Cannot locate resource 'mainwindow.xaml'.
Posted
Comments
Sergey Alexandrovich Kryukov 2-May-11 0:51am    
After the problem is resolved I can see it's the interesting experience. My 5 for this question.
--SA

This code looks correct, in principle; the problem is in the assembly being loaded.

One line which is a suspect is "CreateInstance". It would create instance of some type, which may have not the same name as the entry point. You may want to look my very robust design of working with plug-ins and finding custom entry point instead. I describe its skeleton here:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

Please ignore everything related to re-loadable stuff and Application Domain. You probably only need to load your plug-in assembly only once, so this is not a problem at all.

The exception "Exception has been thrown by the target of an invocation" is very typical for invocation of something which will through exception.

To find out what's wrong, create the assembly to be loaded on disc and try to reference this assembly in the some other project directly and than call its implementation of entry point; in this way it will be shown what's wrong more explicitly under debugger.

Putting a WPF assembly using XAML in a library is not so trivial task. If the assembly you're referencing it trying to create and run the WPF application, it should contain the entry point like this:

C#
//assuming class MyApplication : Application {}:
MyApplication application = new MyApplication(/*...*/);
//assuming MyMainWindow : Window {/*...*/}
application.MainWindow = new MyMainWindow();
application.MainWindow.Icon = //...
application.MainWindow.Show();
application.Run();


Is that what you are doing? I recently developed a system where WPF application is run in the class library compiled in a separate assembly; it worked out perfectly.

—SA
 
Share this answer
 
v3
Comments
Ying Chan 1-May-11 23:42pm    
Thanks for your comprehensive reply! That is exactly what I am aiming to do!
I shall attempt your solution of building the assembly into a library later today and get back to you.
Sergey Alexandrovich Kryukov 2-May-11 0:21am    
You're welcome.
If you think it's useful or just going in right direction, please consider formally accepting this answer (green button).
Thank you.
--SA
Ying Chan 1-May-11 23:53pm    
Just to clarify, I am toying with encryption where the WPF App in question is encrypted. A second exe accepts a key input which is used to decrypt the file, before loading the byte stream into memory and executes the program.
Sergey Alexandrovich Kryukov 2-May-11 0:24am    
I got it. Since you decrypted it and place onto byte array, it is a valid assembly and further steps should be exactly as it you would use LoadFrom, right? Just in case, try to test it all first with LoadFrom and then without encryption...
--SA
Sergey Alexandrovich Kryukov 2-May-11 0:26am    
I don't think I understand what encryption will give you. Just a protection of your code from reverse engineering? But the cracker can get to the code where you load decrypted assembly and save the decrypted code to file; after that this library still can be reverse-engineered. Do I miss anything?
--SA
Maybe this could happen if the the mainwindow.xaml contain any resources that are located in a separate assembly.
 
Share this answer
 
Comments
Ying Chan 1-May-11 22:47pm    
I tested the above code with an empty wpf app and got the same exception. I don't think its due to that
Sergey Alexandrovich Kryukov 1-May-11 22:51pm    
I also don't think it is. However, it's hard to say without looking at your code. There can be different reasons.

Please look at my answer. I did similar things recently; it worked for me.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900