我有一个网站,可以根据用户需求即时编译一个类并将 dll(名为 Equation.dll)存放在网站的子目录中。管理员可以随时重新编译。但是,一旦创建了类的实例,就会显示消息“该进程无法访问该文件,因为它正被另一个进程使用”。
据我了解,解决此问题的唯一方法是从不同的 AppDomain 创建实例。下次管理员需要重新编译时,我需要卸载这个 AppDomain 并且(我认为)一切都会好起来的。(也许我的假设是错误的?)
坦率地说,我无法创建此类的实例来挽救我的生命。诚然,我在这里有点不知所措......所以我在任何事情上都在摇摆不定。此时我的片段是:
AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = HttpContext.Current.Server.MapPath("~/equationcache/");
ads.ApplicationBase = HttpContext.Current.Server.MapPath("~/equationcache/");
AppDomain appDomain = AppDomain.CreateDomain("EquationDomain", null, ads);
Object wrapper = appDomain.CreateInstance("Equation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", "Cnn.CostModel.Business.CalculationEngine");
“equationcache”文件夹是 dll 被编译到的位置(尽管我为了好玩而尝试将它放在“bin”中,但也没有成功)。调用中的程序集名称似乎是正确的(并且它适用于 appDomain.Load)。调用中的类型名称似乎是正确的。根据日志,会发生以下情况:
=== Pre-bind state information ===
LOG: User = xxx\xxx
LOG: DisplayName = Cnn.CostModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Users/xxx/Documents/Visual Studio 2008/Projects/CnnCostModel/CnnCostModels/equationcache/
LOG: Initial PrivatePath = C:\Users\xxx\Documents\Visual Studio 2008\Projects\ACnnCostModel\CnnCostModels\equationcache\Calling assembly : Equation, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\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:/Users/xxx/Documents/Visual Studio 2008/Projects/CnnCostModel/CnnCostModels/equationcache/Cnn.CostModel.DLL.
LOG: Attempting download of new URL file:///C:/Users/xxx/Documents/Visual Studio 2008/Projects/CnnCostModel/CnnCostModels/equationcache/Cnn.CostModel/Apa.CostModel.DLL.
LOG: Attempting download of new URL file:///C:/Users/xxx/Documents/Visual Studio 2008/Projects/CnnCostModel/CnnCostModels/equationcache/Cnn.CostModel.EXE.
LOG: Attempting download of new URL file:///C:/Users/xxx/Documents/Visual Studio 2008/Projects/CnnCostModel/CnnCostModels/equationcache/Cnn.CostModel/Cnn.CostModel.EXE.
我不明白为什么它在“equationcache”目录中寻找不同形式的“Cnn.CostModel”。诚然,调用代码位于 bin 文件夹中的“Cnn.CostModel.dll”中。我需要做什么才能获取“Equation.dll”文件?
还是我完全偏离了这项努力?非常沮丧。任何帮助将不胜感激。