簡體   English   中英

將程序集加載到AppDomain中但無法加載文件或程序集異常

[英]Loaded assembly into AppDomain but getting Could not load file or assembly exception

我試圖動態加載一些像這樣的程序集:

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循環中的app域中。 如何解決此異常?

  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);
}

這要求已加載所有依賴項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM