AppDomain Unload killing Parent AppDomain

后端 未结 1 2046
执笔经年
执笔经年 2021-01-05 02:21

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 ou

相关标签:
1条回答
  • 2021-01-05 02:52

    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);
    

    where ModuleAdapter inherits MarshalByRefObject. Then:

    adapter.Execute(moduleAssembly , moduleType);
    

    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.

    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.

    0 讨论(0)
提交回复
热议问题