首页 文章

跨目录的AppDomain依赖项

提问于
浏览
8

我正在创建一个插件系统,我正在为每个插件创建一个AppDomain . 每个插件都有自己的目录及其主要程序集和引用 . 除了我的接口程序集之外,我的插件加载器将加载主程序集(因此插件可以与应用程序交互) .

创建AppDomain:

this.appDomain = AppDomain.CreateDomain("AppDomain", null, new AppDomainSetup {
    ApplicationBase = pluginPath,
    PrivateBinPath = pluginPath,
});

加载程序集:

this.appDomain.Load(myInterfaceAssembly.GetName(true));

var assemblies = new List<Assembly>();

foreach (var assemblyName in this.assemblyNames) {
    assemblies.Add(this.appDomain.Load(assemblyName));
}

assemblyName 的格式是程序集的文件名,没有".dll" .

问题是 AppDomain.Load(assemblyName) 抛出异常:

无法加载文件或程序集'[[assemblyName]],Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一 . 该系统找不到指定的文件 .

[[assemblyName]] 的所有依赖项都是:

  • 在目录 pluginPath 内,

  • 已加载的 myInterfaceAssembly ,或

  • 在GAC中(例如mscorelib) .

显然,我没有做正确的事情 . 我试过了:

  • 使用 this.appDomain.CreateInstanceAndUnwrap 继承MarshalByRefObject并使用 LoadAssembly 方法创建对象以加载程序集 . 我得到一个异常,说当前程序集(包含代理类)无法加载(找不到文件,如上所述),即使我手动调用 this.appDomain.Load(Assembly.GetExecutingAssembly().GetName(true)) .

  • AssemblyResolve 处理程序附加到 this.appDomain . 我'm met with the same exception as in (1), and manually loading doesn' t帮忙 .

  • 首先通过将其依赖项加载到 this.appDomain 来递归加载程序集 . 这不起作用,但我怀疑我的代码是正确的:

private static void LoadAssemblyInto(AssemblyName assemblyName, AppDomain appDomain) {
    var assembly = Assembly.Load(assemblyName);

    foreach (var referenceName in assembly.GetReferencedAssemblies()) {
        if (!referenceName.FullName.StartsWith("MyProject")) {
            continue;
        }

        var loadedAssemblies = appDomain.GetAssemblies();

        if (loadedAssemblies.Any((asm) => asm.FullName == referenceName.FullName)) {
            continue;
        }

        LoadAssemblyInto(referenceName, appDomain);
    }

    appDomain.Load(assembly.GetName(true));
}

如何加载我的插件程序集及其依赖项在该插件的目录中,同时还加载当前目录中的一些程序集?

注意:插件可能(可能将)引用的程序集已加载到当前域中 . 如果需要,可以跨域共享(性能优势?简单性?) .

融合日志:

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: Where-ref bind. Location = C:/MyProject/bin/Debug/MyProject.Library.DLL
LOG: Appbase = file:///C:/PluginDir
LOG: Initial PrivatePath = C:\PluginDir
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyProject.Library.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\MyProject\bin\Debug\MyProject.Library.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: MyProject.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: Re-apply policy for where-ref bind.
LOG: Where-ref bind Codebase does not match what is found in default context. Keep the result in LoadFrom context.
LOG: Binding succeeds. Returns assembly from C:\MyProject\bin\Debug\MyProject.Library.dll.
LOG: Assembly is loaded in LoadFrom load context.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: Where-ref bind. Location = C:\MyProject\bin\Debug\MyProject.exe
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyProject.exe.
LOG: Assembly download was successful. Attempting setup of file: C:\MyProject\bin\Debug\MyProject.exe
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: Re-apply policy for where-ref bind.
LOG: Where-ref bind Codebase matches what is found in default context. Keep the result in default context.
LOG: The post-policy assembly reference requires probing again.
LOG: Switch from LoadFrom context to default context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Binding succeeds. Returns assembly from C:\MyProject\bin\Debug\MyProject.exe.
LOG: Assembly is loaded in default load context.
LOG: Where-ref bind Codebase matches what is found in default context. Keep the result in default context.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Binding succeeds. Returns assembly from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll.
LOG: Assembly is loaded in default load context.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = MyProject.resources, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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:///C:/MyProject/bin/Debug/en-US/MyProject.resources.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/en-US/MyProject.resources/MyProject.resources.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/en-US/MyProject.resources.EXE.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/en-US/MyProject.resources/MyProject.resources.EXE.
LOG: All probing URLs attempted and failed.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = MyProject.resources, Version=1.0.0.0, Culture=en, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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:///C:/MyProject/bin/Debug/en/MyProject.resources.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/en/MyProject.resources/MyProject.resources.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/en/MyProject.resources.EXE.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/en/MyProject.resources/MyProject.resources.EXE.
LOG: All probing URLs attempted and failed.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = MyProject.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, processorArchitecture=MSIL
 (Fully-specified)
LOG: Appbase = file:///C:/PluginDir
LOG: Initial PrivatePath = C:\PluginDir
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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:///C:/PluginDir/MyProject.Library.DLL.
LOG: Attempting download of new URL file:///C:/PluginDir/MyProject.Library/MyProject.Library.DLL.
LOG: Attempting download of new URL file:///C:/PluginDir/MyProject.Library.EXE.
LOG: Attempting download of new URL file:///C:/PluginDir/MyProject.Library/MyProject.Library.EXE.
LOG: All probing URLs attempted and failed.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:28 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = MyProject
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: MyProject | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : Microsoft.VisualStudio.HostingProcess.Utilities, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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:///C:/MyProject/bin/Debug/MyProject.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyProject/MyProject.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyProject.EXE.
LOG: Assembly download was successful. Attempting setup of file: C:\MyProject\bin\Debug\MyProject.exe
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Binding succeeds. Returns assembly from C:\MyProject\bin\Debug\MyProject.exe.
LOG: Assembly is loaded in default load context.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = MyPlugin
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: MyPlugin | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/PluginDir
LOG: Initial PrivatePath = C:\PluginDir
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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:///C:/PluginDir/MyPlugin.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\PluginDir\MyPlugin.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: MyPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Binding succeeds. Returns assembly from C:\PluginDir\MyPlugin.dll.
LOG: Assembly is loaded in default load context.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = MyPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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:///C:/MyProject/bin/Debug/MyPlugin.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyPlugin/MyPlugin.DLL.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyPlugin.EXE.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/MyPlugin/MyPlugin.EXE.
LOG: All probing URLs attempted and failed.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = Microsoft.VisualStudio.HostingProcess.Utilities, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
 (Fully-specified)
LOG: Appbase = file:///C:/PluginDir
LOG: Initial PrivatePath = C:\PluginDir
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Download of application configuration file was attempted from file:///C:/MyProject/bin/Debug/MyProject.vshost.exe.Config.
LOG: Found application configuration file (C:\MyProject\bin\Debug\MyProject.vshost.exe.Config).
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Post-policy reference: Microsoft.VisualStudio.HostingProcess.Utilities, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
LOG: Found assembly by looking in the GAC.
LOG: Binding succeeds. Returns assembly from C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll.
LOG: Assembly is loaded in default load context.

*** Assembly Binder Log Entry  (12/24/2010 @ 11:03:29 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\MyProject\bin\Debug\MyProject.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Null-Laptop-PC\Null-Laptop
LOG: DisplayName = Ionic.Zip.Reduced, Version=1.9.1.5, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c
 (Fully-specified)
LOG: Appbase = file:///C:/MyProject/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MyProject.vshost.exe
Calling assembly : MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\MyProject\bin\Debug\MyProject.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: Post-policy reference: Ionic.Zip.Reduced, Version=1.9.1.5, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/MyProject/bin/Debug/Ionic.Zip.Reduced.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\MyProject\bin\Debug\Ionic.Zip.Reduced.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: Ionic.Zip.Reduced, Version=1.9.1.5, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c
LOG: Binding succeeds. Returns assembly from C:\MyProject\bin\Debug\Ionic.Zip.Reduced.dll.
LOG: Assembly is loaded in default load context.

3 回答

  • 3

    我再次尝试了一个尝试过的解决方案:

    使用this.appDomain.CreateInstanceAndUnwrap从MarshalByRefObject继承并使用LoadAssembly方法创建对象以加载程序集 . 我得到一个异常,说当前程序集(包含代理类)无法加载(文件未找到,如上所述),即使我手动调用this.appDomain.Load(Assembly.GetExecutingAssembly() . GetName(true)) .

    我发现 CreateInstanceFromAndUnwrap 可以接受汇编文件的路径,这使事情变得简单:

    private T CreateOnAppDomain<T>() {
        var type = typeof(T);
    
        return (T) this.appDomain.CreateInstanceFromAndUnwrap(
            type.Assembly.Location,
            type.FullName
        );
    }
    
    public void LoadAssemblies() {
        var assemblyLoader = CreateOnAppDomain<AssemblyLoader>();
    
        assemblyLoader.LoadAssembly(myInterfaceAssembly.GetName(true));
    
        foreach (var assemblyName in this.assemblyNames) {
            assemblyLoader.LoadAssembly(new AssemblyName(assemblyName));
        }
    }
    
    class AssemblyLoader : MarshalByRefObject {
        public void LoadAssembly(AssemblyName name) {
            Assembly.Load(name);
        }
    }
    

    这解决了我的AppDomain创建问题 . (现在让其他一切工作!)

    谢谢大家的帮助;我非常感谢你的投入 .

  • 9

    我相信当它尝试将程序集加载到主 AppDomain 而不是您明确创建的域时发生异常 . 这是使用 AppDomain.Load 的副作用,这会导致程序集被加载到当前域和您创建的域中 . 由于程序集仅在您创建的域的bin路径(插件目录)中,因此您将获得异常 .

    从与AppDomain.Load相关的MSDN:

    程序集将加载到两个域中,因为Assembly不是从MarshalByRefObject派生的,因此无法封送Load方法的返回值 . 相反,公共语言运行库会尝试将程序集加载到调用应用程序域中 .

  • 2

    我有类似的问题 . 您需要自定义用于查找程序集的方法 . 可能需要修改Console.Debug语句才能编译,但这里是一般要点:

    private static void SetupResolvingAdditionalThirdPartyDlls()
    {
      AppDomain.CurrentDomain.AssemblyResolve += ResolveAssemblies;
    }
    
    private static Assembly ResolveAssemblies(object sender, ResolveEventArgs args)
    {
      Assembly assembly = null;
      bool foundAssembly = false;
    
      Console.DebugFormat("Received request for the following dll {0}", args.Name);
      int idx = args.Name.IndexOf(',');
      if (idx > 0)
      {
        string partialName = args.Name.Substring(0, idx);
        string dllName = partialName + ".dll";
        string exeName = partialName + ".exe";
    
        string searchDirectory = "locationOfDirectoryToSearch";
        // Add other directories that you want to search here
    
    
        List<string> directorySearch = new List<string>
        {
          CombinePath(searchDirectory, dllName),
          CombinePath(searchDirectory, exeName),
          // Include the other directories here to this list adding both the dll and exe.
        };
    
        foreach (string fileName in directorySearch)
        {
          if (File.Exists(fileName))
          {
            Console.DebugFormat("Found dll {0} at {1}", args.Name, fileName);
            foundAssembly = true;
            assembly = Assembly.LoadFrom(fileName);
            break;
          }
        }
    
        if (assembly == null)
        {
          if (!foundAssembly)
          {
            foreach (string fileName in directorySearch)
            {
              Console.DebugFormat("Could not find dll {0} in any search path used {1}", args.Name, fileName);
            }
          }
          else
          {
            Console.DebugFormat("Could not load dll {0}", args.Name);
          }
        }
      }
    
      return assembly;
    }
    

相关问题