Solved

Dynamically Loading/Unloading assembly

Posted on 2012-03-21
3
374 Views
Last Modified: 2012-04-14
I have an executable called Updater.exe that updates the files for our main program (Main.exe). We have to use Updater.exe to update Main.exe because if Main.exe tried to update itself some of the files would be in use.

The problem comes into play if we need to update the Updater.exe file because it shares a dll with Main.exe called InstallCommon.dll. This dll contains a lot of the code for logging into our system and to duplicate would be a huge hassle.

Updater.exe   <------- InstallCommon.dll --------> Main.exe

So what we're left with is if we try to update Updater.exe we can't update the InstallCommon.dll file because Updater.exe will have it in use. But we need this file to be up to date because Main.exe uses it.

So what I've done is to dynamically load the InstallCommon.dll at runtime, do what I need to do with it and then unload it so it's not locked by the executable. I've found many ways to dynamically load an assembly, but the only way I can find to unload it is by creating a new AppDomain object and loading the assembly into there, then unloading at the AppDomain level.

The problem with this is that it's VERY slow to create a new AppDomain. Sometimes taking up to 4 mintues on certain machines. When setting break points I've found that it's actually just one line of code below that is causing the delay. It's the line that creates the new AppDomain instance.

Here is my code below. Is there a better way to do this?

  Dim AppSetup As AppDomainSetup = AppDomain.CurrentDomain.SetupInformation
        Dim NewDomain As AppDomain = AppDomain.CreateDomain("NewDomain", AppDomain.CurrentDomain.Evidence, AppSetup)

        ' THIS IS THE PROBLEM LINE
        Dim obj As System.Runtime.Remoting.ObjectHandle = NewDomain.CreateInstance(GetType(LoadMyAssembly).Assembly.FullName, GetType(LoadMyAssembly).FullName)

        Dim loader As LoadMyAssembly = CType(obj.Unwrap, LoadMyAssembly)
        loader.LoadAssembly("InstallCommon.dll")

        ' The PEClientConfig.xml file should be located in the same directory
        ' as the running executable. However, if this is the first time this
        ' exe is running, we will have to create it.
        Dim ClientConfigPath = Path.Combine(_pVaultBinPath, "PEClientConfig.xml")
        Dim ClientUpdatePath As String = loader.ExecuteStaticMethod("pVaultUpdater_LoadClientUpdatePath", New Object() {ClientConfigPath})

        AppDomain.Unload(NewDomain)

        Return ClientUpdatePath

Open in new window

0
Comment
Question by:PaperlessEnvironments
  • 2
3 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37751200
0
 

Accepted Solution

by:
PaperlessEnvironments earned 0 total points
ID: 37825076
The problem ended up being due to me changing the executable name on disk to something different than the Assembly name in my application settings. For some reason when the new app domain is being created it's looking for the old name... the file relating to the old name did not exist... which produced the error... which produced the delay. For some reason the error was being swallowed up which is why I didn't figure this out sooner.
0
 

Author Closing Comment

by:PaperlessEnvironments
ID: 37845950
It's the solution.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now