我正在尝试将dll加载到控制台应用程序中,然后将其卸载并完全删除文件。我遇到的问题是,在自己的AppDomain中加载dll的行为在父AppDomain中创建了一个引用,因此不允许我销毁dll文件,除非我完全关闭程序。对于让这段代码工作有什么想法吗?
string fileLocation = @"C:\Collector.dll";
AppDomain domain = AppDomain.CreateDomain(fileLocation);
domain.Load(@"Services.Collector");
AppDomain.Unload(domain);
顺便说一句,我也试过这个代码,但也没有运气。
string fileLocation = @"C:\Collector.dll";
byte[] assemblyFileBuffer = File.ReadAllBytes(fileLocation);
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = Environment.CurrentDirectory;
domainSetup.ShadowCopyFiles = "true";
domainSetup.CachePath = Environment.CurrentDirectory;
AppDomain tempAppDomain = AppDomain.CreateDomain("Services.Collector", AppDomain.CurrentDomain.Evidence, domainSetup);
//Load up the temp assembly and do stuff
Assembly projectAssembly = tempAppDomain.Load(assemblyFileBuffer);
//Then I'm trying to clean up
AppDomain.Unload(tempAppDomain);
tempAppDomain = null;
File.Delete(fileLocation);
发布于 2010-04-27 01:33:15
好,我在这里解决了我自己的问题。显然,如果你调用AppDomain.Load,它会向你的父AppDomain注册它。所以简单地说,答案就是根本不引用它。这是一个网站的链接,它展示了如何正确地设置。
发布于 2015-11-21 22:39:07
这应该很简单:
namespace Parent {
public class Constants
{
// adjust
public const string LIB_PATH = @"C:\Collector.dll";
}
public interface ILoader
{
string Execute();
}
public class Loader : MarshalByRefObject, ILoader
{
public string Execute()
{
var assembly = Assembly.LoadFile(Constants.LIB_PATH);
return assembly.FullName;
}
}
class Program
{
static void Main(string[] args)
{
var domain = AppDomain.CreateDomain("child");
var loader = (ILoader)domain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);
Console.Out.WriteLine(loader.Execute());
AppDomain.Unload(domain);
File.Delete(Constants.LIB_PATH);
}
}
}
https://stackoverflow.com/questions/2715104
复制相似问题