Reputation: 20310
I am trying to dynamically load some assemblies like this:
using (var svc = new MyClient()) // MyClient is proxy to a WCF service
{
var bytecode = svc.GetAssembly();
var assembly = Assembly.Load(bytecode);
var dependencies = svc.GetDependencies();
foreach (var dependency in dependencies)
{
Assembly.Load(dependency.Bytecode);
Console.WriteLine("loaded {0}", asm.FullName);
}
var type = assembly.GetExportedTypes().First();
var ctor = type.GetConstructor(new Type[0]);
var obj = ctor.Invoke(new object[0]); // get an exception here
}
However, I get an exception when ctor is invoked, and CLR tries to load dependent assemblies. I have loaded the dependencies into the app domain in the for loop. How can I fix this exception?
System.Reflection.TargetInvocationException occurred
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
at MyClass.Program.Main(String[] args) in f:\Client\Program.cs:line 25
InnerException: System.IO.FileNotFoundException
HResult=-2147024894
Message=Could not load file or assembly 'Asm2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Source=Asm1
FileName=Asm2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
FusionLog==== Pre-bind state information ===
LOG: User = mymachine\me
LOG: DisplayName = Asm2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///f:/Client/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: f:\Client.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///f:/Client/bin/Debug/Asm2.DLL.
LOG: Attempting download of new URL file:///f:/Client/bin/Debug/Asm2/Asm2.DLL.
LOG: Attempting download of new URL file:///f:/Client/bin/Debug/Asm2.EXE.
LOG: Attempting download of new URL file:///f:/Client/bin/Debug/Asm2/Asm2.EXE.
StackTrace:
at MyNamespace.MyClass..ctor()
InnerException:
Upvotes: 2
Views: 5312
Reputation: 3123
I have solved a similar problem by handling AppDomain.CurrentDomain.AssemblyResolve
:
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
}
This requires that all dependencies are already loaded.
Upvotes: 9