C# ASP.NET:CreateDomain(…).CreateInstanceAndUnwrap(…)或Assembly.LoadFrom(…).GetExportedTypes()引发FileNotFoundException 简介

C# ASP.NET:CreateDomain(…).CreateInstanceAndUnwrap(…)或Assembly.LoadFrom(…).GetExportedTypes()引发FileNotFoundException 简介,c#,asp.net,reflection,.net-assembly,appdomain,C#,Asp.net,Reflection,.net Assembly,Appdomain,我正在尝试加载一些程序集并从中获取类型。。。 程序集的路径是 bin/Plugin1 我还试图为他们创建一个应用程序域,并将他们加载到那里。基本步骤是: 1。创建域: domain = AppDomain.CreateDomain( "Plugins" ) AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" ); AppDomain.CreateDomain("Plugins").Creat

我正在尝试加载一些程序集并从中获取类型。。。 程序集的路径是

bin/Plugin1
我还试图为他们创建一个应用程序域,并将他们加载到那里。基本步骤是:

1。创建域:

domain = AppDomain.CreateDomain( "Plugins" )
AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" );
AppDomain.CreateDomain("Plugins").CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, ... );
2。创建一个工人

worker = (Scan)domain.CreateInstanceAndUnwrap(
                           Assembly.GetExecutingAssembly().FullName, typeof(Scan ).FullName );
AppDomain.CurrentDomain.SetData( "APPBASE", "C:\\...\\Project\\bin\\Plugin1" );
AppDomain.CreateDomain( "Plugins", null, AppDomain.CurrentDomain.SetupInformation).CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, ... );
3。加载程序集

worker.Scan( [PATH_TO_DLL] )
 Assembly.LoadFrom( [PATH_TO_DLL] ).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: 
4。负载类型

worker.Scan( [PATH_TO_DLL] )
 Assembly.LoadFrom( [PATH_TO_DLL] ).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: 
我尝试了我能想到的任何方法,但在第2步或第4步的某个地方,我总是会遇到FileNotFoundException

我试过的 1。应用程序基路径:

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

2。具有AppDomainSetup的新域

worker = (Scan)domain.CreateInstanceAndUnwrap(
                           Assembly.GetExecutingAssembly().FullName, typeof(Scan ).FullName );
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。已使用的程序集。加载自

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

5。Web.Config探测

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

任何帮助都将不胜感激

你的问题帮助我找到了解决“类似”情况的方法。我想这可能会帮助其他人。我正在用asp.net写一个网站

我在步骤2中成功地使用了CreateInstanceFromAndUnwrap。 与CreateInstanceFromAndUnwrap一样,您可以指定所需程序集的位置

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