簡體   English   中英

ASP.NET: CreateDomain(...).CreateInstanceAndUnwrap(...) OR Assembly.LoadFrom(...).GetExportedTypes() 拋出 FileNotFoundException

[英]ASP.NET: CreateDomain(...).CreateInstanceAndUnwrap(...) OR Assembly.LoadFrom( ... ).GetExportedTypes() throws FileNotFoundException

介紹

我正在嘗試加載一些程序集並從中獲取類型......程序集的路徑是

bin/Plugin1

我也在嘗試為他們創建一個應用程序域並將它們加載到那里。 基本步驟是:

1. 創建域:

domain = AppDomain.CreateDomain( "Plugins" )

2. 創建一個工人

worker = (Scan)domain.CreateInstanceAndUnwrap(
                           Assembly.GetExecutingAssembly().FullName, typeof(Scan ).FullName );

3. 加載組件

worker.Scan( [PATH_TO_DLL] )

4. 負載類型

 Assembly.LoadFrom( [PATH_TO_DLL] ).GetExportedTypes()

我嘗試了任何我能想到的方法,但我總是在第 2 步或第 4 步中的某個地方遇到 FileNotFoundException。

我試過的

1.ApplicationBase路徑:

AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" );
AppDomain.CreateDomain("Plugins").CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, ... );

拋出 FileNotFoundException

2. 使用 AppDomainSetup 的新域

AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" );
AppDomain.CreateDomain( "Plugins", null, AppDomain.CurrentDomain.SetupInformation).CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, ... );

拋出 FileNotFoundException

3. 使用 CreateInstanceFromAndUnwrap

var assembly = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Plugin1", Path.GetFileName( Assembly.GetExecutingAssembly().Location ) );
AppDomain....CreateInstanceFromAndUnwrap(assembly, ... );

工作了但是...

4.使用Assembly.LoadFrom

var asm = Assembly.LoadFrom( [PATH_TO_ASSEMBLY] );//works
var types = asm.GetExportedTypes();

拋出 FileNotFoundException

5. Web.Config 探測

<probing privatePath="bin;bin/Plugin1;" />

沒有幫助任何地方...

這讓我相信,在不解決實際問題的情況下強制從路徑加載程序集(程序集似乎已加載到 iis 路徑)並沒有解決任何問題,我沒有走在解決問題的正確道路上......

我得到的例外

1. CreateInstance 異常

System.IO.FileNotFoundException was unhandled by user code
  HResult=-2147024894
  Message=Could not load file or assembly 'Plugin.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=Plugin.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog== == Pre-bind state information == =
LOG: DisplayName = Plugin.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/.../Web/PluginWeb.heTest/PluginWeb.heTest/PluginWeb.heTest/bin/Plugin/
LOG: Initial PrivatePath = C:\...\Web\PluginWeb.heTest\PluginWeb.heTest\PluginWeb.heTest\bin
Calling assembly : (Unknown).
== =
LOG: This bind starts in default load context.
LOG: Configuration file C:\...\Web\PluginWeb.heTest\PluginWeb.heTest\PluginWeb.heTest\bin\Plugin\web.config does not exist.
LOG: No application configuration file found.
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\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:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/Plugin.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/Plugin.Libs.Base/Plugin.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/Plugin.Libs.Base.EXE.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/Temporary ASP.NET Files/root/993f86a2/2dca2916/Plugin.Libs.Base/Plugin.Libs.Base.EXE.
StackTrace:
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(String assemblyName, String typeName)
       at System.AppDomain.CreateInstance(String assemblyName, String typeName)
       at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
       at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
       at Plugin.Libs.Base.Schema.mySchemaManager.<>c__DisplayClass6.<Scan>b__3() in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 202
       at Plugin.Libs.Base.mySys.WriteLog(poSysLogScale scale, Action method, String message, Object[] args) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Sys.cs:line 514
       at Plugin.Libs.Base.Schema.mySchemaManager.Scan(Action'1 processView) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 184
       at Plugin.Libs.Base.Schema.mySchemaManager.<>c__DisplayClass1.<Load>b__0() in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 176
       at Plugin.Libs.Base.mySys.WriteLog(poSysLogScale scale, Action method, String message, Object[] args) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Sys.cs:line 514
       at Plugin.Libs.Base.Schema.mySchemaManager.Load(Action'1 processView) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 175
       at Plugin.Libs.Base.myApplication.Load(Action'1 processView) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Application.cs:line 141
       at Plugin.Web.Forms.Fakes.FakeAppContext.Get(String aliasFileName, String userName, String password) in c:\...\PluginWeb.heTest\PluginWeb.heTest\Plugin.Web.Forms\Fakes\AppContext.cs:line 25
       at Plugin.Web.Forms.Controllers.BaseController..ctor() in c:\...\PluginWeb.heTest\PluginWeb.heTest\Plugin.Web.Forms\Controllers\BaseController.cs:line 30
       at PluginWeb.heTest.Classes.BaseController..ctor()
       at PluginWeb.heTest.Controllers.HomeController..ctor()
  InnerException:

2. .GetExportedTypes() 的異常

System.IO.FileNotFoundException was caught
  HResult=-2147024894
  Message=Could not load file or assembly 'PluginChild.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=PluginChild.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog== == Pre-bind state information == =
LOG: DisplayName = PluginChild.Libs.Base, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///c:/windows/system32/inetsrv/
LOG: Initial PrivatePath = NULL
Calling assembly : PluginChild.Obj.MGM.DataObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
== =
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: No application configuration file found.
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\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:/windows/system32/inetsrv/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///c:/windows/system32/inetsrv/PluginChild.Libs.Base/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///c:/windows/system32/inetsrv/PluginChild.Libs.Base.EXE.
LOG: Attempting download of new URL file:///c:/windows/system32/inetsrv/PluginChild.Libs.Base/PluginChild.Libs.Base.EXE.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base/PluginChild.Libs.Base.DLL.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base.EXE.
LOG: Attempting download of new URL file:///C:/.../PluginWeb.heTest/PluginWeb.heTest/bin/Apps/Obj/PluginChild.Libs.Base/PluginChild.Libs.Base.EXE.

  StackTrace:
       at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
       at System.Reflection.RuntimeAssembly.GetExportedTypes()
       at Plugin.Libs.Base.Schema.SchemaManager.MarsalScan.ScanAssembly(ScanResult res, String item) in c:\...\Plugin.Libs.Base\Plugin.Libs.Base\Schema\Manager.cs:line 81
  InnerException: 

任何幫助將不勝感激!

您的問題幫助我找到了解決“類似”情況的方法。 認為這可能會幫助別人。 我在asp.net 中編寫了一個網站。

我在步驟 2 中使用 CreateInstanceFromAndUnwrap 取得了成功。與 CreateInstanceFromAndUnwrap 一樣,您可以指定所需程序集的位置。

worker = (Scan)domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(Scan ).FullName);

暫無
暫無

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

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