本文介绍了AppDomain卸载杀父母的AppDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦搞清楚的东西了我的 AppDomain.Unload(...)通话。我有code从my早期的问题。事实证明,我是执行了几个步骤,显然,我不需要。不过,我相当肯定,被创建,然后一个AppDomain时举行的集合:

I am having trouble figuring something out about my AppDomain.Unload(...) call. I have a detailed explanation with code from my earlier question. As it turns out, I was performing a couple of steps that apparently, I don't need to. However, I am fairly certain that when an AppDomain is created and then held in a collection:

private static Dictionary<string , AppDomain> HostDomains;

void StartNewDomain(string domainName)
{
    AppDomain domain = AppDomain.CreateDomain(domainName);
    HostDomains[domainName] = domain;
}

...当你用它做,你必须卸载它:

...when you are done with it, you must unload it:

if (HostDomains.ContainsKey(domainName))
{
    AppDomain.Unload(HostDomains[domainName]);
    HostDomains.Remove(domainName);
}

然后从集合中删除域。

then remove domain from the collection.

当我卸载域,但是,整个应用程序的结束。如果我删除卸载,一切都很好......我们只是留下了从集合删除域。但我担心我的孩子的AppDomain是的没有的真正卸载。它有可能最终获得GC'd我猜,但是这并没有给我一个温暖的模糊。

When I unload the domain, however, the entire application is ending. If I remove the unload, all is well...and we are simply left with removing the domain from the collection. But I fear that my child AppDomain is not really unloaded. It may eventually get GC'd I guess, but that doesn't give me a warm fuzzy.

孩子的AppDomain组件(Windows窗体应用程序)的异步通过接口(IModule的)开始是在我继承 MarshalByRefObject的适配器类引用。我想知道如果这个参考的IModule的start()(该插件模块组件实现)是不是封送的(因为我的实现)正确。所以,当关断()方法被调用时,整个应用程序死亡。我应该让我的IModule的一个抽象类,而不是因此它应该继承MBR呢?不解...

The child AppDomain assembly (a Windows Form application) is started asynchronously via an interface (IModule) that is referenced in my adapter class which inherits MarshalByRefObject. I am wondering if this reference to IModule's Start() (which the plugin module assembly implements) is not marshaling properly (because of my implementation). So, when the Shutdown() method is called, the entire application dies. Should I make my IModule an abstract class instead so it should inherit MBR as well? Puzzled...

在看我的code:

// instances the module for access to the module's Start() method
    IModule module = (IModule)domain.CreateInstanceAndUnwrap(
    ModuleManager.Modules[modName].Name,
    ModuleManager.Modules[modName].EntryPoint.FullName);

...我担心的是,由于IModule的是一个接口,即使我创造一个子域的一个实例,该组件被泄漏到我的主要的AppDomain。所以,当我尝试卸载子域,两个域正在卸载。这将是正确的吗?什么将可能是最好的解决办法,以提供启动()及停止()通过MBR(适配器)对象的方法?

...my fear is that since IModule is an interface, even though I am creating an instance in a child domain, the assembly is leaking into my main AppDomain. Therefore, when I attempt to unload the child domain, both domains are being unloaded. Would this be correct? And what would likely be the best solution to provide Start() & Stop() methods via the MBR (adapter) object?

更新:看到我的回答下面的变化 - 中国好了,没有泄漏 - 一切都继承了MBR:

UPDATE: see my answer below for changes --
Okay, there is no leaking -- everything inherits MBR:

  1. 主持人:MarshalByRefObject的 - 实例ModuleAdapter在新的AppDomain
  2. ModuleAdapter:MarshalByRefObject的 - 的IModule接口,接口方式(启动,停止)
  3. MyModulePlugin:MarshalByRefObject的 - Application.Run(myForm的)

我是不是做错了还是?我已经试过几件事情,它只是似乎是错误的或不完整的。当我告诉ModuleAdapter关机,它会调用 AppDomain.Unload(AppDomain.CurrentDomain)键,主机域名将停止为好。我仍然得到在应用程序退出一些第一次机会异常。但形式(myForm会)已被告知.Close()。

Am I doing something wrong still? I've tried several things and it just seems to be wrong or incomplete. When I tell the ModuleAdapter to shutdown, it calls AppDomain.Unload(AppDomain.CurrentDomain) and the Host domain stops as well. I am still getting some first chance exceptions on the application exit. But the form (myForm) has been told to .Close().

所以,我仍然在寻找这样做的正确方法...

So, I am still looking for the correct way of doing this...

推荐答案

正如我怀疑,在主域的的IModule接口实例化导致泄漏。为了做到这一点适当的:

As I suspected, instancing with the IModule interface in the primary domain causes a leak. In order to do this properly:

AppDomain domain = AppDomain.CreateDomain(domainName);
HostDomains[domainName] = domain;  // put in collection

ModuleAdapter adapter = (ModuleAdapter)domain.CreateInstanceAndUnwrap(asmName , typeName);

在这里ModuleAdapter继承 MarshalByRefObject的。然后:

adapter.Execute(moduleAssembly , moduleType);

在ModuleAdapter类:

Inside the ModuleAdapter class:

public void Execute(string Name, string EntryPoint)
{
    module  = (IModule)AppDomain.CurrentDomain.CreateInstanceAndUnwrap(Name , EntryPoint);
}

我欢迎评论或额外的答案一个更好的办法。

I do welcome comments or additional answers for a better way.

移动实例化的ModuleAdapter下课后,我们仍然有问题,AppDomain.Unload杀死整个应用程序。我想知道,这是因为在模块插件,我们使用 Application.Run(myForm的) - 那么当我们关闭,我们称之为myForm.Close()。显然,这会关闭,所以我在想,应用程序,如果myForm.Close()也'卸载'的AppDomain中。

After moving the instancing to the ModuleAdapter class, we are still having the issue with AppDomain.Unload killing the entire application. I was wondering if this is because in the module plugin we are using Application.Run(myForm) - then when we shutdown we call myForm.Close(). Obviously this shuts down the application so I was wondering if the myForm.Close() also 'unloads' the AppDomain.

这篇关于AppDomain卸载杀父母的AppDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:30
查看更多