将程序集加载到子AppDomain中并释放dll文件

发布于 2024-11-28 15:19:32 字数 798 浏览 0 评论 0原文

我有一个子应用程序域,我想在启动和发布文件时加载一些 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-12-05 15:19:32

创建应用程序域时使用卷影副本。它将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文