1

我通过以下方式将程序集加载到 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我看到的值"/"

我该如何解决这个问题?

4

1 回答 1

0

我将 ApplicationPath 设置为等于放置程序集的路径:

appdomainSetup.ApplicationPath = @"c:\path";

因为这个程序集被 Web 项目引用,所以我希望我的程序集包含在与我的 Web 项目相同的目录中,这样我就可以使用相对路径(很像bin文件夹的工作方式)。但是,无论出于何种原因,这都是行不通的。到目前为止,指定服务器上存在的完整路径是唯一有效的解决方案。

于 2013-03-29T19:35:23.253 回答