将程序集加载到子AppDomain中并释放dll文件
我有一个子应用程序域,我想在启动和发布文件时加载一些 dll 库,以便任何人都可以删除它们。
在启动时,我
Loader al = (Loader)domain.CreateInstanceAndUnwrap(
typeof(Loader).Assembly.FullName,
typeof(Loader).FullName);
al.Load(path)
为下面的课程做。
class Loader : MarshalByRefObject
{
internal void Load(string path)
{
Assembly assembly;
try
{
assembly = Assembly.Load(File.ReadAllBytes(path));
}
catch (Exception) { return; }
}
internal UseType(string fullyQualifiedTypeName)
{
Type userType = Type.GetType(fullyQualifiedTypeName);
}
}
后来我调用 UseType
并获得了正确的类型,但我无法再删除该文件,因为就好像子应用程序域已锁定该 dll。
基本上我想要实现的是在启动时缓存程序集文件,然后使用 GetType 调用,以便释放实际的 dll 文件。
真的有可能实现这样的目标吗?
I have child app-domain where I want to load some dll libraries on start-up and release files so that anybody will be able to delete them.
On start-up I do
Loader al = (Loader)domain.CreateInstanceAndUnwrap(
typeof(Loader).Assembly.FullName,
typeof(Loader).FullName);
al.Load(path)
for the following class.
class Loader : MarshalByRefObject
{
internal void Load(string path)
{
Assembly assembly;
try
{
assembly = Assembly.Load(File.ReadAllBytes(path));
}
catch (Exception) { return; }
}
internal UseType(string fullyQualifiedTypeName)
{
Type userType = Type.GetType(fullyQualifiedTypeName);
}
}
Later I invoke UseType
and I get the correct type but I am not able to delete the file any more because it is as if the child app-domain has locked the dll.
Basically what I want to achieve is to cache the assembly file on start-up and later use GetType
calls so that the actual dll file will be released.
Is it really possible to achieve something like this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建应用程序域时使用卷影副本。它将 dll 复制到缓存中,任何人都可以与文件系统交互。
Topshelf 通过我们的搁置来实现此目的(那么所有内容都位于它自己的应用程序域中) - https://github.com/Topshelf/Topshelf/blob/v2.3/src/Topshelf/Model/ShelfReference.cs#L126。
更新:Topshelf 不再执行此操作,但更新了指向执行此操作的版本的链接。
Use shadow copy when you create the App Domain. That copies the dlls into a cache and anyone can interact with the file system.
Topshelf does this with our shelving (everything lives in it's own app domain then) - https://github.com/Topshelf/Topshelf/blob/v2.3/src/Topshelf/Model/ShelfReference.cs#L126.
Update: Topshelf no longer does this, but updated a link to a version which did.