簡體   English   中英

在appdomain中加載程序集

[英]Loading assemblies in appdomain

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

正如您在調試跟蹤中可能已經注意到的那樣,運行時正在嘗試從多個目錄讀取dll。 似乎它們都不包含指定的程序集。

您正在使用“ SearchOption.AllDirectories”,因此您的程序集可能來自諸如“ plugin / plugin1”或“ extensions / plugin1”之類的子目錄。 在這種情況下,您應該將此目錄添加到域PrivateBinPath http://msdn.microsoft.com/zh-cn/library/system.appdomainsetup.privatebinpath(v=vs.110).aspx中 在您的示例中,您僅添加了

暫無
暫無

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

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