RE: Assembly.Load() and updating DLLs on the fly

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Dan,
Somebody correct me if I'm wrong, but I believe that even if you spin up a
new AppDomain with all the plumbing as was described, you cannot "replace"
loaded assemblies in it with ones that have the same identity. Once an
assembly is loaded into an AppDomain, the only way to unload it is to tear
down the appDomain, create a new one, and load your "revised" assemblies into
that as "first occupiers".
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



"Dan Dorey" wrote:

Hi,

I've successfully built an application that loads plugins dynamically
at runtime. I now have the requirement that I update these DLLs while
the program is still running. Our app is 24/7 and it would be
beneficial if we could update functionality without restarting the
services (this is time consuming).

The first thing I realized was that any DLLs that are loaded
dynamically (or otherwise) are locked by the application. So it could
never be as simple as just overwritting the plugins. To get around
this, I create a plugin manager that would make a copy of DLLs before
loading them. Then when a user updates the DLLs, the plugin manager
detects this, makes another copy of the DLLs and tries to load them.

Note that we want to be able to update the DLLs with an assembly that
has the same identity.

Originally I was using Assembly.LoadFrom() to load everything up as it
does the dependency resolution for me. However as is stated in the
docs for LoadFrom():

The LoadFrom method has the following disadvantages. Consider using
Load instead.
* If an assembly with the same identity is already loaded, LoadFrom
returns the loaded assembly even if a different path was specified.

Clearly I can't use LoadFrom anymore, but from the way the above
statement was described, I assumed that using Load() would not have
this disadvantage.

I've used the Assembly.Load() method in the following fashion however
just like LoadFrom it returns me the original instance of the
assembly.

foreach (FileInfo curFileInfo in fileList)
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = curFileInfo.FullName;
ret.Add(Assembly.Load(assemblyName));
}

Am I wrong in assuming Load() does not have this disadvantage or am I
using it incorrectly?

The only other option I've read about is using Assembly.LoadFile().
This method does not do dependency resolution for me. I wouldn't even
know where to start to handle depenecy resolution myself. There are
depenedent DLLs in other directories....

Any thoughts or suggestions?

Thanks,
Dan


.



Relevant Pages

  • Re: loading assembly
    ... think of on the Load. ... AppDomain not the default AppDomain. ... R> If I copy the assembly to the local folder the above ...
    (microsoft.public.dotnet.general)
  • Difficulty loading an assembly into a new AppDomain
    ... I am attempting to load a managed C++ DLL dynamically into a separate ... AppDomain so that I can unload the AppDomain later. ... different directory from all of the other assemblies. ... It would be nice to keep MyAssembly in a separate directory because we've ...
    (microsoft.public.dotnet.framework.interop)
  • RE: Usage of AppDomain.CreateInstanceFromAndUnwrap method
    ... The reason for other failures is that Controls are not inherited from ... so the program will attempt to load them into the ... Create an AppDomain, do a CreateInstanceAndUnwrap on ... Assemblies in to a seprate ...
    (microsoft.public.dotnet.framework.clr)
  • Problem loading assemblies in seperate AppDomain
    ... I'm currently working on a program that dynamically loads assemblies from a ... RemoteLoader I'm getting all sorts of errors stating that it can't find the ... strikes me as weird because I was absolutely certain to use the new AppDomain ... even more so when I call the Load() function off of my newly created ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Remoting and AppDomains
    ... class in the main AppDomain, it's value would be null in the other AppDomain ... i need to say which assembly to load into the AppDomain ... >> the app then copies a these assemblies to a cache folder along with my ... I then AppDomain.ExecuteAssembly my RemotingClient. ...
    (microsoft.public.dotnet.languages.csharp)