Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamically Loading/Unloading assembly

Posted on 2012-03-21
3
Medium Priority
?
385 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
 

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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

810 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