?
Solved

Dynamically Loading/Unloading assembly

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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:…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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