Loading Assembly Problem  
Author Message
Jonaspp





PostPosted: .NET Base Class Library, Loading Assembly Problem Top

Hello ppl!

I have this program, it is a Updater, it will download many files to a temp dir and then replace the old ones.

Every time before update, I must check the program version, loading an assembly and getting the classType, then getting a static field value. No problem here, but, this assembly file will also be replaced in the new downloaded version, and I won't be able to replace the file because it hasn't been unloaded and the domain still using it, even if I create another domain and unload it before replacing. Any help Please!!

The loading version code is like this:

// Creating a new domain
AppDomain productionDomain = AppDomain.CreateDomain("ProductionDomain");

// Loading the assembly
Assembly productionAssembly = Assembly.LoadFile(productionDLLFile);

// Loading into the domain
productionDomain.Load(productionAssembly.GetName());

// Getting the class type
Type productionType = productionAssembly.GetType("Version.ProductionVersion");

// Getting the field info
FieldInfo versionFieldInfo = productionType.GetField("CURRENT_VERSION", BindingFlags.Public | BindingFlags.Static);

// Getting the field value
version = versionFieldInfo.GetValue(null).ToString();

// Unloading the domain

AppDomain.Unload(productionDomain);

I also tried to copy the file to a temp dir and loading the assembly from this temp file, it won't work.

Thanks




.NET Development29  
 
 
nobugz





PostPosted: .NET Base Class Library, Loading Assembly Problem Top

You are loading the assembly into the startup AppDomain too. That only gets unloaded when you program ends. Meanwhile, Windows has a lock on the DLL. You need a radically different approach to fix this.

First, define an interface in a 3rd assembly that lets you retrieve the version info. Add a reference to it in both your .exe project and your production DLL project. Have the ProductVersion class implement this interface. After loading the production DLL into the domain, use AppDomain.CreateInstance to create the ProductVersion class and cast the return value to the interface type. Call the interface method to obtain the version.


 
 
Jonaspp





PostPosted: .NET Base Class Library, Loading Assembly Problem Top

thanks nobugz...

Problem solved with another solution, but it is very similar yours.