ASP.NET: CreateDomain(...).CreateInstanceAndUnwrap(...) OR A



简介

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

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.使用过的程序集。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:...WebPluginWeb.heTestPluginWeb.heTestPluginWeb.heTestbin
Calling assembly : (Unknown).
== =
LOG: This bind starts in default load context.
LOG: Configuration file C:...WebPluginWeb.heTestPluginWeb.heTestPluginWeb.heTestbinPluginweb.config does not exist.
LOG: No application configuration file found.
LOG: Using host configuration file: C:WindowsMicrosoft.NETFramework64v4.0.30319aspnet.config
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.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.BasePlugin.Libs.BaseSchemaManager.cs:line 202
       at Plugin.Libs.Base.mySys.WriteLog(poSysLogScale scale, Action method, String message, Object[] args) in c:...Plugin.Libs.BasePlugin.Libs.BaseSys.cs:line 514
       at Plugin.Libs.Base.Schema.mySchemaManager.Scan(Action'1 processView) in c:...Plugin.Libs.BasePlugin.Libs.BaseSchemaManager.cs:line 184
       at Plugin.Libs.Base.Schema.mySchemaManager.<>c__DisplayClass1.<Load>b__0() in c:...Plugin.Libs.BasePlugin.Libs.BaseSchemaManager.cs:line 176
       at Plugin.Libs.Base.mySys.WriteLog(poSysLogScale scale, Action method, String message, Object[] args) in c:...Plugin.Libs.BasePlugin.Libs.BaseSys.cs:line 514
       at Plugin.Libs.Base.Schema.mySchemaManager.Load(Action'1 processView) in c:...Plugin.Libs.BasePlugin.Libs.BaseSchemaManager.cs:line 175
       at Plugin.Libs.Base.myApplication.Load(Action'1 processView) in c:...Plugin.Libs.BasePlugin.Libs.BaseApplication.cs:line 141
       at Plugin.Web.Forms.Fakes.FakeAppContext.Get(String aliasFileName, String userName, String password) in c:...PluginWeb.heTestPluginWeb.heTestPlugin.Web.FormsFakesAppContext.cs:line 25
       at Plugin.Web.Forms.Controllers.BaseController..ctor() in c:...PluginWeb.heTestPluginWeb.heTestPlugin.Web.FormsControllersBaseController.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:WindowsMicrosoft.NETFramework64v4.0.30319aspnet.config
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFramework64v4.0.30319configmachine.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.BasePlugin.Libs.BaseSchemaManager.cs:line 81
  InnerException: 

任何帮助都将不胜感激!

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

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

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

相关内容

  • 没有找到相关文章

最新更新