我通过以下方式将程序集加载到 appdomain 中:
var appdomainSetup = new AppDomainSetup();
//string appBase = Environment.CurrentDirectory;
//string appBase = HttpContext.Current.Request.ApplicationPath;
appdomainSetup.ApplicationBase = _appBase;//System.Web.HttpContext.Current.Request.ApplicationPath: Injected into cstor
System.Diagnostics.Debug.WriteLine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
appdomainSetup.DisallowBindingRedirects = false;
appdomainSetup.DisallowCodeDownload = true;
appdomainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
appDomain = AppDomain.CreateDomain("myAppdomain", null, appdomainSetup);
在哪里BLASSEMBLYLOCATION = "c:\path\myAssembly.dll";//for now just hard coded path
然后我用另一种方法从这个程序集中获取一个类型:
var helperObject= appDomain.CreateInstanceAndUnwrap("myssembly.NameContinued", "myNamespace.BL.Helper") as Helper;//ERROR LINE HERE
这一切都在单元测试中工作得很好。但是,当通过 Web 应用程序运行时,这会出现错误:
无法加载文件或程序集“myssembly.NameContinued”或其依赖项之一。
FileNotFoundException
我可以通过不指定在我的单元测试中重现
appdomainSetup.ApplicationBase
我怀疑这个问题与我设置为 ApplicationBase 的目录有关。在网络程序中,我传递了HttpContext.Current.Request.ApplicationPath
我看到的值"/"
。
我该如何解决这个问题?