0

I got loading problem and can't resolve it. The Error I got is as follows:

Could not load file or assembly 'BuildTest, Version=1.0.0.4, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"BuildTest, Version=1.0.0.4, Culture=neutral, PublicKeyToken=null"

The code I wrote is as follows:

var buildTestPath = @"D:\BuildTest\bin\Debug";

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = "BuildTest";
setup.ShadowCopyFiles = "true";
setup.PrivateBinPath = buildTestPath;

AppDomain domain = AppDomain.CreateDomain("MyDomain", AppDomain.CurrentDomain.Evidence, setup);

var assemblyFile = Path.Combine(buildTestPath, "BuildTest.dll");
var assemblyName = AssemblyName.GetAssemblyName(assemblyFile);
var assembly = domain.Load(assemblyName);

Type myType = assembly.GetType("BuildTest.Class1");
MethodInfo myMethod = myType.GetMethod("MethodA");
object obj = Activator.CreateInstance(myType);
myMethod.Invoke(obj, null);

AppDomain.Unload(domain);

Could anyone help me out?

Thanks

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
Joon w K
  • 777
  • 1
  • 6
  • 27
  • Did you check if you have BuildTest.dll in this path- D:\BuildTest\bin\Debug? – Souvik Ghosh Oct 10 '17 at 01:28
  • The `PrivateBinPath` must be a subdirectory of the `AppDomain.BaseDirectory`. Also see this [answer](https://stackoverflow.com/a/6629826/21567). If that is the case, use the `fuslogvw.exe` to get details on the assembly loading errors. – Christian.K Oct 10 '17 at 04:15

1 Answers1

0

AppDomain.Load should only be used to load an assembly into the current AppDomain.

To load the assembly (and create an instance of the type) into your newly created domain, the AppDomain class exposes methods like CreateInstanceAndUnwrap, which you should be using here.

Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • I created proxy like this : ProxyClass c = (ProxyClass)(domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().CodeBase, typeof(ProxyClass).FullName)); Console.WriteLine(c == null); var dd = c.GetType(); var assemblyFilePath = Path.Combine(buildTestPath, "BuildTest.dll"); var assm = c.GetAssembly(assemblyFilePath); var types = assm.ExportedTypes; AppDomain.Unload(domain); – Joon w K Oct 10 '17 at 04:44
  • I made proxy and created assembly thru proxy but I got same result. What I want to do is that: I want to create separate domain using appdomainsetup and load some.dll and get types and delete some.dll after unloading. I have tried many different way, but nothing works for me. – Joon w K Oct 10 '17 at 04:53