4
votes

simple question, probably easy for you to answer.

I have a dll named "MigrationSteps.dll" in the same output folder of my application. What I want to do, is to load this assembly in a new AppDomain and execute a method on an instance of a class inside this DLL.

Here's my code

       string migrationStepsDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MigrationSteps.dll");
        AppDomainSetup appDomainSetup = new AppDomainSetup() { PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory };
        Evidence evidence = AppDomain.CurrentDomain.Evidence;

        AppDomain appDomain = AppDomain.CreateDomain("MigrationAppDomain", evidence, appDomainSetup);

 //NOT WORKING
        Assembly assembly = appDomain.Load(@"C:\Output\Debug\OptimeToolbench\MigrationSteps.dll");

        //WORKING
        Assembly assembly = Assembly.LoadFrom(@"C:\Output\Debug\OptimeToolbench\MigrationSteps.dll"); ****works.

        //This part works well
        Type type = assembly.GetType("MigrationSteps.Foo");
        object foo = Activator.CreateInstance(type);
        MethodInfo methodInfo = type.GetMethod("HelloWorld");
        methodInfo.Invoke(foo, null);
        AppDomain.Unload(appDomain);

Everytime the line indicated as not working throws a

FileNotFoundException

.

Why's that ?

Thanks for you time.

2
Can you enable the Fusion logger and see what the first is looking for and where? - Preet Sangha
It won't be necessary, the solution was answer. Thanks for your time. - esylvestre
Just want to point out that this code will load into the primary AppDomain & lock the target assembly for the lifetime of the application. To prevent that (and actually unload the target), you need to create a proxy object which is loaded dynamically, loads the target dynamically (this code), and finally then shuffles serializable data (if need be) back to the calling app. - JoeBrockhaus

2 Answers

3
votes

Add "C:\Output\Debug\OptimeToolbench\" to the PrivateBinPath of the AppDomain. Also do not pass in the file name, pass in the assembly name -- I'm assuming that would be MigrationSteps.

3
votes

appDomain.Load(string) takes in an assembly name (the strong-name) - NOT the path of where the file is on disk!