tags:

views:

267

answers:

3

Ok, heres the rundown...

I have a service that is running and it references a dll that is changed a lot. This service is hopefully going to have multiple clients hitting it at once so it would be inefficient to shut it down and recomplile/reload with a new reference. I was wondering if there was anyway the program could pretty much auto-detect a dll with a later version and just drop the old one and load the new one without having to be shut down.

A: 

The only way to do this is move the code that uses the assembly to its own AppDomain. You can shutdown the AppDomain and restart it with the new assembly.

See this question: How to reload an assembly in C# for a .NET Application Domain?

280Z28
A: 

The Managed Extensibility Framework will allow you to do this.

Matt Lacey
I do not think so. Once an assembly is loaded, it cannot be unloaded unless the entire domain is unloaded. MEF might help you to locate the exact version you want, but as of .NET 3.5 there is no way to unload what's already there
mfeingold
Combine use of MEF with the ApplicationRestartRecoveryManager to easily force a restart of the app then.
Matt Lacey
Well, it is an application restart - isn't it, and jared wants to avoid it.
mfeingold
+4  A: 

This can be difficult to achieve in a .Net application. Once a DLL is loaded into a particular AppDomain there is no way to unload the DLL from the AppDomain. The only way to get the DLL out of the process is to unload the AppDomain itself.

You could achieve what you're trying to accomplish by having the DLL loaded into a secondary AppDomain, and restarting that AppDomain with the new DLL when you detect a change. This also involves using some advanced shadow copy features though to allow the DLL to be deleted while being used by the process.

JaredPar
Bit of an aside comment, but is this still the case in .NET 4.0?
ParmesanCodice
@ParmesanCodice, yes
JaredPar