Solved

Dynamically Loading/Unloading assembly

Posted on 2012-03-21
3
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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