在appdomain中加载程序集

时间:2014-06-14 21:11:58

标签: c# asp.net-mvc

我目前正在为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

1 个答案:

答案 0 :(得分:0)

正如您在调试跟踪中可能已经注意到的那样,运行时正在尝试从多个目录中读取您的dll。似乎它们都不包含指定的程序集。

您正在使用“SearchOption.AllDirectories”,因此您的程序集可能来自子目录,例如“plugin / plugin1”或“extensions / plugin1”。在这种情况下,您应该将此目录添加到您的域PrivateBinPath http://msdn.microsoft.com/en-us/library/system.appdomainsetup.privatebinpath(v=vs.110).aspx。在您的示例中,您只需添加