【发布时间】:2014-06-14 21:11:57
【问题描述】:
我目前正在为 asp.net mvc 构建一个插件框架。所有插件都被复制到一个目录中,现在我尝试加载它们。起初我将它们全部添加到当前的 appdomain 中,但是当有不同版本的程序集时会产生冲突。因此,我正在考虑为每个插件构建一个 appdomain。标准 dll 在下面的代码中加载,但我添加的用于测试加载不同版本的自定义类没有加载。这是我使用的代码。
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);
}
由于某种原因,我构建的自定义类未加载。这是我得到的例外:
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
【问题讨论】:
-
请检查MSDN - AppDomain.Load:“此方法应仅用于将程序集加载到当前应用程序域中。”和相关问题,以澄清您的问题。
标签: c# asp.net-mvc