Solved

Dynamically Loading/Unloading assembly

Posted on 2012-03-21
3
373 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 video discusses moving either the default database or any database to a new volume.

708 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

15 Experts available now in Live!

Get 1:1 Help Now