我通过以下方式将程序集加载到应用程序域中:
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”或其依赖项之一。
我可以在我的单元测试中通过不指定
appdomainSetup.ApplicationBase
我怀疑这个问题与我设置为ApplicationBase的目录有关。在web程序中,我传递HttpContext.Current.Request.ApplicationPath的值,我认为它是"/"。
我怎样才能解决这个问题?
发布于 2013-03-29 19:35:23
我将ApplicationPath设置为放置程序集的路径:
appdomainSetup.ApplicationPath = @"c:\path";因为这个程序集是由web项目引用的,所以我希望我的程序集与我的web项目包含在同一个目录中,这样我就可以使用相对路径(就像bin文件夹的工作一样)。然而,无论出于什么原因,这都是行不通的。到目前为止,在服务器上指定完整路径是唯一可行的解决方案。
https://stackoverflow.com/questions/15710038
复制相似问题