2014-06-14 45 views
0

我目前正在爲asp.net mvc構建一個插件框架。所有插件都被複制到一個目錄,現在我嘗試加載它們。起初我將它們全部添加到當前的appdomain中,但是當存在不同版本的程序集時會產生衝突。因此,我正在考慮爲每個插件構建一個appdomain。標準的dll被加載到下面的代碼中,但是我添加的用於測試不同版本加載的自定義類未加載。這是我使用的代碼。在appdomain中加載程序集

 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 
+0

請檢查[MSDN - AppDomain.Load](http://msdn.microsoft.com/zh-cn/library/36az8x58%28v=vs.110%29.aspx): 「此方法只能用於將程序集加載到當前應用程序域中。」和相關的問題來澄清你的問題。 –

回答

0

正如你可能在你的調試跟蹤通知,運行正試圖從幾個目錄讀取您的DLL。看起來他們都沒有包含指定的Assembly。

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

+0

你真的指出了我的問題。我所看到的是,appdomain沒有使用我提供的私有路徑。必須弄清楚爲什麼它沒有使用它。 – Patrick

相關問題