Load an assembly dynamically in a new App Domain
-
How do I load an Assembly dynamically in a NEW appdomain? The assembly is in bytes[] array. Gurpreet
-
How do I load an Assembly dynamically in a NEW appdomain? The assembly is in bytes[] array. Gurpreet
You should look for articles that deals with reflection and plugins. YOu should be able to get the code you need from there. I have a little plugin system on my page too (oldow it does not load an assembly into a new appdomain): http://cwizo.slogamedev.net/index.php?i=showcase&s=programs Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
-
How do I load an Assembly dynamically in a NEW appdomain? The assembly is in bytes[] array. Gurpreet
KaurGurpreet wrote:
How do I load an Assembly dynamically in a NEW appdomain?
Just you would normally, but you need to call it from within that AppDomain. ;)
-
You should look for articles that deals with reflection and plugins. YOu should be able to get the code you need from there. I have a little plugin system on my page too (oldow it does not load an assembly into a new appdomain): http://cwizo.slogamedev.net/index.php?i=showcase&s=programs Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
_________________________________________________________ The Assembly that I need to load: _________________________________________________________ namespace WindowsApplication4 { public class TestDll : MarshalByRefObject,interfaces.Test { public string HelloWorld() { return "Hello World"; } } } _________________________________________________________ interfaces.Test is in a seprate dll. code is as below: _________________________________________________________ namespace interfaces { public interface Test { string HelloWorld(); } } _________________________________________________________ The code that I am using to load: _________________________________________________________ AppDomain currentDomain = AppDomain.CreateDomain("mydomain1"); currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver); domain.CreateInstance("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "WindowsApplication4.TestDll"); static Assembly MyResolver(object sender, ResolveEventArgs args) { Assembly assembly; AppDomain domain = (AppDomain)sender; // Actually the assembly in bytes will come from database so temporarily loading it as below: byte[] rawAssembly = loadFile("d:\\TestAssembly.dll"); assembly = domain.Load(rawAssembly); return assembly; } I DO NOT FACE ANY PROBLEM IN LOADING THE ASSEMBLY WHEN IT IS NOT IMPLEMENTING THE INTERFACE. IT WORKS FINE IN THAT CASE. Gurpreet -- modified at 3:12 Wednesday 15th February, 2006
-
_________________________________________________________ The Assembly that I need to load: _________________________________________________________ namespace WindowsApplication4 { public class TestDll : MarshalByRefObject,interfaces.Test { public string HelloWorld() { return "Hello World"; } } } _________________________________________________________ interfaces.Test is in a seprate dll. code is as below: _________________________________________________________ namespace interfaces { public interface Test { string HelloWorld(); } } _________________________________________________________ The code that I am using to load: _________________________________________________________ AppDomain currentDomain = AppDomain.CreateDomain("mydomain1"); currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver); domain.CreateInstance("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "WindowsApplication4.TestDll"); static Assembly MyResolver(object sender, ResolveEventArgs args) { Assembly assembly; AppDomain domain = (AppDomain)sender; // Actually the assembly in bytes will come from database so temporarily loading it as below: byte[] rawAssembly = loadFile("d:\\TestAssembly.dll"); assembly = domain.Load(rawAssembly); return assembly; } I DO NOT FACE ANY PROBLEM IN LOADING THE ASSEMBLY WHEN IT IS NOT IMPLEMENTING THE INTERFACE. IT WORKS FINE IN THAT CASE. Gurpreet -- modified at 3:12 Wednesday 15th February, 2006
-
I am guessing but I think that you should add refrence to the Interface dll in your TestDLL but you should really post some error or something. Q:What does the derived class in C# tell to it's parent? A:All your base are belong to us!
I get the following error: {"Could not load file or assembly 'TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"} reference of interface.dll is added in Testdll. Testdll can not be compiled without the reference :) Gurpreet