ASP.NET:CreateDomain(...).CreateInstanceAndUnwrap(...) 或 Assembly.LoadFrom(...).GetExportedTypes() 抛
Posted
技术标签:
【中文标题】ASP.NET:CreateDomain(...).CreateInstanceAndUnwrap(...) 或 Assembly.LoadFrom(...).GetExportedTypes() 抛出 FileNotFoundException【英文标题】:ASP.NET: CreateDomain(...).CreateInstanceAndUnwrap(...) OR Assembly.LoadFrom( ... ).GetExportedTypes() throws FileNotFoundException 【发布时间】:2015-07-30 12:55:49 【问题描述】:简介
我正在尝试加载一些程序集并从中获取类型... 程序集的路径是
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:
任何帮助将不胜感激!
【问题讨论】:
【参考方案1】:您的问题帮助我找到了解决“类似”情况的方法。认为这可能对其他人有帮助。我用asp.net写了一个网站。
我在第 2 步中成功使用了 CreateInstanceFromAndUnwrap。 与 CreateInstanceFromAndUnwrap 一样,您可以指定所需程序集的位置。
worker = (Scan)domain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(Scan ).FullName);
【讨论】:
以上是关于ASP.NET:CreateDomain(...).CreateInstanceAndUnwrap(...) 或 Assembly.LoadFrom(...).GetExportedTypes() 抛的主要内容,如果未能解决你的问题,请参考以下文章