vendredi 18 avril 2014

.net - c# charge différentes versions d'assembly pour le même projet - Stack Overflow


I'm creating some tool what performs several operations like NUnit. Inside this tool I open .dll assembly and invoke methods form it to run some test.


Everything is going OK till the time I need to reload .dll withour program restart. The idea is that when tool is run we copy required assembly to some temporary folder and invoke from there. If I need to reload I copy another one to another temporary folder and try to load newly copied from another folder and load to previous assembly object


  ExecutingAssembly = Assembly.LoadFrom(AssemblyFullPath); 

But my problem is that after I change AssemblyFullPath to new one and call Assembly.LoadFrom it returns just old assembly what was loaded first time but not the second one! Maybe the problem is that we cannot load several assemblies with different versions? What is the solution?




The CLR does support loading multiple versions of strongly named assemblies into the same AppDomain. This only works though if your assemblies are strongly named and each one has a different version than the other.


I'm guessing it's more likely that you are dealing with unsigned assemblies. If that is the case then what you're asking for isn't really possible. Once a given assembly is loaded into an AppDomain it will remain there until the AppDomain is unloaded. To get this to work you will have to abstract out all of the work around the loaded assemblies into a separate AppDomain and use a new AppDomain for every assembly




To expand on JaredPar's answer, you will need to create a new AppDomain and use Remoting to communicate between the two.


Check out http://msdn.microsoft.com/en-us/library/kwdt6w2k(v=vs.85).aspx to help get you started.



I'm creating some tool what performs several operations like NUnit. Inside this tool I open .dll assembly and invoke methods form it to run some test.


Everything is going OK till the time I need to reload .dll withour program restart. The idea is that when tool is run we copy required assembly to some temporary folder and invoke from there. If I need to reload I copy another one to another temporary folder and try to load newly copied from another folder and load to previous assembly object


  ExecutingAssembly = Assembly.LoadFrom(AssemblyFullPath); 

But my problem is that after I change AssemblyFullPath to new one and call Assembly.LoadFrom it returns just old assembly what was loaded first time but not the second one! Maybe the problem is that we cannot load several assemblies with different versions? What is the solution?



The CLR does support loading multiple versions of strongly named assemblies into the same AppDomain. This only works though if your assemblies are strongly named and each one has a different version than the other.


I'm guessing it's more likely that you are dealing with unsigned assemblies. If that is the case then what you're asking for isn't really possible. Once a given assembly is loaded into an AppDomain it will remain there until the AppDomain is unloaded. To get this to work you will have to abstract out all of the work around the loaded assemblies into a separate AppDomain and use a new AppDomain for every assembly



To expand on JaredPar's answer, you will need to create a new AppDomain and use Remoting to communicate between the two.


Check out http://msdn.microsoft.com/en-us/library/kwdt6w2k(v=vs.85).aspx to help get you started.


0 commentaires:

Enregistrer un commentaire