views:

59

answers:

2

Hi, 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  A: 

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

Robert Seder
+1. I guess the it expects the display name of the assembly which might or might not be a full strong name - but you are correct, it will not accept a path.
andras
That's right, thank you very much.Need to give the answer to code4life, he answered with the good answer first.
esylvestre
No worries - glad you're all set now!
Robert Seder
+1  A: 

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.

code4life
Already done (for the PrivateBinPath), look on line #2.But you were also right, thank you very much.
esylvestre
Oops, sorry man! But glad that you resolved it!
code4life