How can I reload DLL after update (C#)

Topics: Updater Application Block
Jan 5, 2006 at 6:47 AM
originally posted by: TorbenVallund

Hi.

I am using the Application Updater Block 2.0 with the following change: We do not want to have two exe-files and therefore we only updates DLLs from the webserver. The only exe-file is the AppStart.

The following code loads the DLL, which has been updated and runs the main form in our application.

// Load the frmMain type from assembly
Type bpsMainFormType = Type.GetType(@"WindowsUI.frmMain,WindowsUI", true);
// Find the default constructor
ConstructorInfo mainFormCtor = bpsMainFormType.GetConstructor(new Type0);
// Create main form by calling constructor
Form bpsMainForm = (Form)mainFormCtor.Invoke(new object0);
// Show main form
Application.Run(bpsMainForm);

However - the user can choose to check for updates in the main form - this will close the main form and download the new DLL using the Application Updater.

After the update the main form should be started again using the NEW updated DLL.

But when the code above is executed again the old DLL is still loaded. The main form from the old DLL will be running even though a new DLL has been downloaded and have replaced the old file.

Is it possible to reload the DLL into memory in some way?

Or is the solution to use AppDomains? - If so, where can I find a simple example of this?

Any suggestions will be highly appreciated.

Best regards, Torben.
Jan 6, 2006 at 3:23 AM
originally posted by: TorbenVallund

A correction: actually the DLLs are downloaded but not copied into the working folder, because the Application Updater cannot overwrite the DLLs. The reason for this is that the DLLs are loaded in the memory.

So actually we do not want to reload the DLLs - we want to unload them before the Application Updater downloads the new DLLs.

If anyone know of a way to unload the DLLs from memory in .NET (C#) we would be happy to get this information!

Maybe this is the reason why the Updater Application Block comes with samples, where an exe.file is downloaded and not only DLLs. But we would very much like only to download DLLs in order to prevent the user from starting the application exe instead of the application updater AppStart exe-file.
Jan 7, 2006 at 9:59 AM
originally posted by: winker9

I am running into the same problem!!

I have a very complex application where 99% of everything is in a DLL, which isn't really that hard to do with .NET.

I am getting nothing but "File in use" event log errors. The only files I can update are "data" files.

I believe this renders UAB unusable for this VERY COMMON application scenario, due to the following:

I thought all modules (EXE and DLLs) were unloaded by the time my process exits, or more specifically, after the WaitForProcessExit() call returns from a separate process!! This is obviously not the case, because the WaitForApplicationExitProcess cannot apply any DLLs used by my application.

Note that this doesn't include any of the UAB or EL files--this is a separate issue that I (and everyone) am working around.

I would also like to repeat the call for anyone knowing the secret incantation to get assemblies unloaded, and not just when CLR feels like doing it. I have a genuine need to do this outside UAB; namely being able to unload plugins from my app and having all their DLLs GONE GONE GONE from my AppDomain!!!!
Jan 10, 2006 at 5:02 AM
originally posted by: matiaswoloski

Unloading an assembly is not that easy. One solution is create your application using a new AppDomain with ShadowCopyFiles property of the AppDomain. Something similar to what ASP.Net does.

Some pointers
http://blogs.msdn.com/suzcook/archive/2003/07/08/57211.aspx
http://blogs.msdn.com/junfeng/archive/2004/02/09/69919.aspx

I will try to put an example in my blog in the following days.

Matias
http://staff.southworks.net/blogs/matiaswoloski