C# 已将程序集加载到AppDomain,但获取无法加载文件或程序集异常

C# 已将程序集加载到AppDomain,但获取无法加载文件或程序集异常,c#,.net,clr,C#,.net,Clr,我正在尝试动态加载一些程序集,如下所示: 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

我正在尝试动态加载一些程序集,如下所示:

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
}
但是,当调用ctor,CLR尝试加载依赖程序集时,我会遇到一个异常。我已将依赖项加载到for循环中的应用程序域中。如何修复此异常

  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: 

我通过处理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);
}

这要求已加载所有依赖项。

堆栈跟踪表明缺少Asm2。您能否检查Asm2程序集是否在此目录中:f:/Client/bin/Debug/您想做什么?我已经编写了许多动态加载程序集和实例化类型的应用程序。这显然是一个依赖性问题,其中Asm2与加载它的项目有关。Asm2不在该目录中。我已经在for循环中将其加载到应用程序域中。还使用Console.WriteLine进行了验证。CLR应针对已加载的程序集进行绑定,而不是搜索文件系统。这就是我想要完成的,我在问。我认为你应该把它复制到那里,以确保这是当前的问题,然后反向工作,看看为什么它试图“再次”加载它。它是否正在寻找旧版本的Asm(1.0.0.0)?使用Assembly.Load(字节[])?CLR不应该与那个绑定。太好了!进行了此修改:私有静态程序集AssemblyResolve(对象发送方,ResolveEventArgs args){return AppDomain.CurrentDomain.GetAssemblys()。其中(a=>a.FullName==args.Name)。SingleOrDefault();}