我试图使用一个临时AppDomain为外部库(引用CrystalDecisions
)加载程序集,然后将其卸载,因此最终我将从内存中卸载CrystalDecisions
dll。
我正在使用Microsoft的ListDLLs来查看CrystalDecisions
dll是否已卸载,并且看到在执行引用它的外部方法之前,这些dll没有被加载,并且一旦执行此方法,这些dll已加载但未从列表中卸载。
下面是用于创建AppDomain,加载程序集并执行该方法,最后将其卸载的代码。
var setupInformation = AppDomain.CurrentDomain.SetupInformation;
setupInformation.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
var appDomainForCrystalExecutor = AppDomain.CreateDomain(string.Empty,
new Evidence(AppDomain.CurrentDomain.Evidence), setupInformation);
var utilsDllPath = Path.Combine(Directory.GetCurrentDirectory(), string.Format("{0}.dll", UtilsNamespace));
var assembly = appDomainForCrystalExecutor.Load(new AssemblyName
{
CodeBase = utilsDllPath
});
var crExecutorType = assembly.GetType(string.Format("{0}.CRExecutor", UtilsNamespace));
var methodInfo = crExecutorType.GetMethod("TryExecuteReport", new Type[] {typeof(CrystalReportVO)});
object crExecutor;
try
{
crExecutor = appDomainForCrystalExecutor
.CreateInstanceFrom(crExecutorType.Assembly.Location, crExecutorType.FullName).Unwrap();
}
catch (Exception ex)
{
AppDomain.Unload(appDomainForCrystalExecutor);
return;
}
Task.Run(() =>
{
var reportExecutedSuccessfuly = (bool) methodInfo.Invoke(crExecutor, new[] {crystalReportVo});
AppDomain.Unload(appDomainForCrystalExecutor);
});
ListDLL中卸载的dll的示例: 0x0000000028090000 0x163000 C:\ Program Files(x86)\ SAP BusinessObjects \ Crystal Reports for .NET Framework 4.0 \ Common \ SAP BusinessObjects Enterprise XI 4.0 \ win32_x86 \ crlov.dll