[英]Loading assemblies in appdomain
I'm currently building a plugin framework for asp.net mvc. 我目前正在为asp.net mvc构建一个插件框架。 All plugins are copied to a directory and now I try to load them.
所有插件都复制到目录中,现在我尝试加载它们。 At first I added them all to the current appdomain but that gives conflicts when there are different versions of assemblies.
首先,我将它们全部添加到当前的appdomain中,但是当存在不同版本的程序集时会产生冲突。 Therefor I was thinking to build a appdomain per plugin.
因此,我正在考虑为每个插件构建一个appdomain。 Standard dll's are loaded in the code below, but the custom class that I added to test loading of different versions is not loaded.
下面的代码中加载了标准dll,但我加载用于测试不同版本加载的自定义类却未加载。 This is the code I use.
这是我使用的代码。
foreach (var directoryInfo in TempPluginFolder.GetDirectories())
{
//AppDomainFactory fact = new AppDomainFactory();
AppDomainSetup setup = new AppDomainSetup();
setup.ShadowCopyFiles = "true";
setup.LoaderOptimization = LoaderOptimization.MultiDomain;
setup.PrivateBinPath = directoryInfo.FullName + ";" + AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
AppDomain domain = AppDomain.CreateDomain(directoryInfo.FullName, AppDomain.CurrentDomain.Evidence,setup);
foreach (var fileInfo in directoryInfo.GetFiles("*.dll", SearchOption.AllDirectories))
{
var assemblyName = AssemblyName.GetAssemblyName(fileInfo.FullName);
domain.Load(assemblyName);
}
appDomains.Add(domain);
}
For some reason my custom class that I builded is not loaded. 由于某种原因,我构建的自定义类未加载。 This is the exception that I get:
这是我得到的例外:
Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Source Error:
Line 112: {
Line 113: var assemblyName = AssemblyName.GetAssemblyName(fileInfo.FullName);
Line 114: domain.Load(assemblyName);
Line 115: }
Line 116: foreach (var fileInfo in directoryInfo.GetFiles("*.dll", SearchOption.AllDirectories))
Source File: c:\Users\Patrick\Documents\Visual Studio 2013\Projects\Project\Project.UI.Web.PluginManager\PreApplicationInit.cs Line: 114
Assembly Load Trace: The following information can be helpful to determine why the assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' could not be loaded.
=== Pre-bind state information ===
LOG: DisplayName = ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///c:/users/patrick/documents/visual studio 2013/Projects/Project/Project.UI.Web.MVC/
LOG: Initial PrivatePath = c:\users\patrick\documents\visual studio 2013\Projects\Project\Project.UI.Web.MVC\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: c:\users\patrick\documents\visual studio 2013\Projects\Project\Project.UI.Web.MVC\web.config
LOG: Using host configuration file: C:\Users\Patrick\Documents\IISExpress\config\aspnet.config
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:/Users/Patrick/AppData/Local/Temp/Temporary ASP.NET Files/root/8aa98896/e1fa08af/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///C:/Users/Patrick/AppData/Local/Temp/Temporary ASP.NET Files/root/8aa98896/e1fa08af/ClassLibrary1/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///c:/users/patrick/documents/visual studio 2013/Projects/Project/Project.UI.Web.MVC/bin/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///c:/users/patrick/documents/visual studio 2013/Projects/Project/Project.UI.Web.MVC/bin/ClassLibrary1/ClassLibrary1.DLL.
LOG: Attempting download of new URL file:///C:/Users/Patrick/AppData/Local/Temp/Temporary ASP.NET Files/root/8aa98896/e1fa08af/ClassLibrary1.EXE.
LOG: Attempting download of new URL file:///C:/Users/Patrick/AppData/Local/Temp/Temporary ASP.NET Files/root/8aa98896/e1fa08af/ClassLibrary1/ClassLibrary1.EXE.
LOG: Attempting download of new URL file:///c:/users/patrick/documents/visual studio 2013/Projects/Project/Project.UI.Web.MVC/bin/ClassLibrary1.EXE.
LOG: Attempting download of new URL file:///c:/users/patrick/documents/visual studio 2013/Projects/Project/Project.UI.Web.MVC/bin/ClassLibrary1/ClassLibrary1.EXE.
Stack Trace:
[FileNotFoundException: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]
System.AppDomain.Load(AssemblyName assemblyRef) +0
Project.UI.Web.PluginManager.PreApplicationInit.LoadAssemblies() in c:\Users\Patrick\Documents\Visual Studio 2013\Projects\Project\Project.UI.Web.PluginManager\PreApplicationInit.cs:114
Project.UI.Web.PluginManager.PreApplicationInit.InitializePlugins() in c:\Users\Patrick\Documents\Visual Studio 2013\Projects\Project\Project.UI.Web.PluginManager\PreApplicationInit.cs:56
[InvalidOperationException: The pre-application start initialization method InitializePlugins on type Project.UI.Web.PluginManager.PreApplicationInit threw an exception with the following error message: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified..]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +556
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516
[HttpException (0x80004005): The pre-application start initialization method InitializePlugins on type Project.UI.Web.PluginManager.PreApplicationInit threw an exception with the following error message: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified..]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9885060
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009
As you might have notice in your debug trace, the runtime is trying to read your dll from several directories. 正如您在调试跟踪中可能已经注意到的那样,运行时正在尝试从多个目录读取dll。 It appears that none of them contains the specified Assembly.
似乎它们都不包含指定的程序集。
Your are using "SearchOption.AllDirectories" so your assembly might come from subdirectory such as "plugin/plugin1" or "extensions/plugin1". 您正在使用“ SearchOption.AllDirectories”,因此您的程序集可能来自诸如“ plugin / plugin1”或“ extensions / plugin1”之类的子目录。 In that case you should add this directory to your domain PrivateBinPath http://msdn.microsoft.com/en-us/library/system.appdomainsetup.privatebinpath(v=vs.110).aspx .
在这种情况下,您应该将此目录添加到域PrivateBinPath http://msdn.microsoft.com/zh-cn/library/system.appdomainsetup.privatebinpath(v=vs.110).aspx中 。 In your example your are only adding the
在您的示例中,您仅添加了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.