Load dll client side
-
Hi, I'm working on extensibility of my Silverlight application and would like to load a dll on the client side and invoke methods based on an interface, can it be done? Any links to good articles? I would prefer to not include it in my build of my application, ie the client can upload extensions, and then invoke them, so its not a referenced library...
____________________________________________________________ Be brave little warrior, be VERY brave
-
Hi, I'm working on extensibility of my Silverlight application and would like to load a dll on the client side and invoke methods based on an interface, can it be done? Any links to good articles? I would prefer to not include it in my build of my application, ie the client can upload extensions, and then invoke them, so its not a referenced library...
____________________________________________________________ Be brave little warrior, be VERY brave
Adriaan Davel wrote:
would like to load a dll on the client side and invoke methods based on an interface, can it be done?
Yes. Given a Stream to an assembly DLL, you can use the AssemblyPart.Load method to load the assembly into the current app domain and get an Assembly object. Then you can use Assembly.CreateInstance to create instances of classes in the assembly.
Stream sourcestream = ...; AssemblyPart assemblypart = new AssemblyPart(); Assembly assembly = assemblypart.Load(sourcestream); IMyInterface myinterfaceobject = assembly.CreateInstance("MyNamespace.SomeClassThatInheritsFromIMyInterface") as IMyInterface;
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi, I'm working on extensibility of my Silverlight application and would like to load a dll on the client side and invoke methods based on an interface, can it be done? Any links to good articles? I would prefer to not include it in my build of my application, ie the client can upload extensions, and then invoke them, so its not a referenced library...
____________________________________________________________ Be brave little warrior, be VERY brave
Here's a version that searches the assembly for the first instance of a class that implements your interface, and if found, creates an instance of that class:
IMyInterface myinterfaceobject = null; Stream sourcestream = ...; AssemblyPart assemblypart = new AssemblyPart(); Assembly assembly = assemblypart.Load(sourcestream); foreach (Type t in assembly.GetTypes()) { if (null != t.GetInterface("IMyInterface", false)) { myinterfaceobject = assembly.CreateInstance(t.FullName) as IMyInterface; break; } } if (myinterfaceobject != null) { // Do something! }
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Adriaan Davel wrote:
would like to load a dll on the client side and invoke methods based on an interface, can it be done?
Yes. Given a Stream to an assembly DLL, you can use the AssemblyPart.Load method to load the assembly into the current app domain and get an Assembly object. Then you can use Assembly.CreateInstance to create instances of classes in the assembly.
Stream sourcestream = ...; AssemblyPart assemblypart = new AssemblyPart(); Assembly assembly = assemblypart.Load(sourcestream); IMyInterface myinterfaceobject = assembly.CreateInstance("MyNamespace.SomeClassThatInheritsFromIMyInterface") as IMyInterface;
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi Mark, Thanks for the reply. Loading the assembly and creating an instance is easy enough, my challenge is getting the assembly to the client side in the best possible way as it will not ship with the .xap file, any ideas around that?
____________________________________________________________ Be brave little warrior, be VERY brave
-
Hi Mark, Thanks for the reply. Loading the assembly and creating an instance is easy enough, my challenge is getting the assembly to the client side in the best possible way as it will not ship with the .xap file, any ideas around that?
____________________________________________________________ Be brave little warrior, be VERY brave
Adriaan Davel wrote:
my challenge is getting the assembly to the client side in the best possible way as it will not ship with the .xap file, any ideas around that?
Maybe try downloading it like you would any resource. For example, Using the WebClient class' OpenReadAsync() method to download the assembly DLL, you'll get a stream in the OpenReadCompleted event which you can use in the sample code I posted.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Adriaan Davel wrote:
my challenge is getting the assembly to the client side in the best possible way as it will not ship with the .xap file, any ideas around that?
Maybe try downloading it like you would any resource. For example, Using the WebClient class' OpenReadAsync() method to download the assembly DLL, you'll get a stream in the OpenReadCompleted event which you can use in the sample code I posted.
Mark Salsbery Microsoft MVP - Visual C++ :java:
I've played with OpenReadAsync and it works well, but it means that I have to download the assembly everytime, which I'm trying to avoid...
____________________________________________________________ Be brave little warrior, be VERY brave
-
I've played with OpenReadAsync and it works well, but it means that I have to download the assembly everytime, which I'm trying to avoid...
____________________________________________________________ Be brave little warrior, be VERY brave
Adriaan Davel wrote:
it means that I have to download the assembly everytime, which I'm trying to avoid...
The XAP file is downloaded every time too, along with every resource not included in the XAP file. Cached versions will be used if available. The only option you have without user intervention is isolated storage.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi, I'm working on extensibility of my Silverlight application and would like to load a dll on the client side and invoke methods based on an interface, can it be done? Any links to good articles? I would prefer to not include it in my build of my application, ie the client can upload extensions, and then invoke them, so its not a referenced library...
____________________________________________________________ Be brave little warrior, be VERY brave
I'm also looking at MEF for this, still not sure if it will work for this (or even how it works :))
____________________________________________________________ Be brave little warrior, be VERY brave
-
Adriaan Davel wrote:
it means that I have to download the assembly everytime, which I'm trying to avoid...
The XAP file is downloaded every time too, along with every resource not included in the XAP file. Cached versions will be used if available. The only option you have without user intervention is isolated storage.
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks, right now I'm trying to understand MEF, else Isolated Storage is probably the right option...
____________________________________________________________ Be brave little warrior, be VERY brave