gpt4 book ai didi

c# - ASP.NET:CreateDomain(...).CreateInstanceAndUnwrap(...) 或 Assembly.LoadFrom(...).GetExportedTypes() 抛出 FileNotFoundException

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:13 27 4
gpt4 key购买 nike

简介

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

bin/Plugin1

我也在尝试为他们创建一个应用程序域并将它们加载到那里。基本步骤是:

<强>1。创建域:

domain = AppDomain.CreateDomain( "Plugins" )

<强>2。创建 worker

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);

关于c# - ASP.NET:CreateDomain(...).CreateInstanceAndUnwrap(...) 或 Assembly.LoadFrom(...).GetExportedTypes() 抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com