而错误的AppDomain卸载。 (异常来自HRESULT:0x80131015),Windows服务里面 [英] Error while unloading appdomain. (Exception from HRESULT: 0x80131015), inside Windows Service

查看:814
本文介绍了而错误的AppDomain卸载。 (异常来自HRESULT:0x80131015),Windows服务里面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个窗口服务这个错误。
这是我在我的问题前面讨论的同样的服务这里



中的代码修改为使用 Parallel.ForEach (我自己的版本,因为这是一个3.5窗口服务)。究其原因,并行使用减少了一个事实,即它只是时间太长,卸载各个领域,并在并行运行它们应被证明是更快的(似乎是即使只有一个,就是在做每个卸载?线程)。



根据其他职位,我只能猜测,这是不知何故到我使用的事实线程池 卸载的AppDomain 秒。 ?我看不出如何避免

 公共部分类SomeService:ServiceBase 
{
私人经理_appDomainManager;

保护覆盖无效调用OnStop()
{
_appDomainManager.Dispose();
}
}

公共类经理:IDisposable的
{
公共无效的Dispose()
{
Log.Debug( 转让);
的Dispose(真);
GC.SuppressFinalize(本);
}

受保护的虚拟无效的Dispose(BOOL处置)
{
如果(_disposed)回报;
如果(处置)
{
//配置管理资源
的Parallel.For(0,appdomains.Length,UnloadAppDomian);
}

_disposed = TRUE;
}
}

私人UnloadAppDomain(INT appDomainIndex);

公共静态类Parallel35
{
公共静态无效的for(int开始,诠释年底,动作< INT>动作)
{
VAR waitHandles =新的WaitHandle [结束 - 开始]。
为(INT J = 0; J< waitHandles.Length; J ++)
{
waitHandles [J] =新的ManualResetEvent(假);
}

的for(int i =启动; I< END;我++)
{
INT I1 = I - 启动;
ThreadPool.QueueUserWorkItem(
状态= GT;
{

{
动作((int)的状态);
}
终于
{
((ManualResetEvent的)waitHandles [I1])设置();
}
},I);
}
WaitHandle.WaitAll的(waitHandles);
}
}


解决方案

我找到了这个bug在出口等待那个从来没有设置的WaitHandle的的AppDomain 取值之一。




如果一个线程不会中止,对于因为它是在执行
非托管代码,或者是因为它在执行finally块,然后CannotUnloadAppDomainException被抛出的时候
A期后例子在
线程原名卸载。




的AppDomain 现在卸载比较快,我的服务很快停止。


I receive this error in a windows service. This is the same service that I've previously discussed in my question here

The code is revised to use Parallel.ForEach (my own version as this is a 3.5 windows service). The reason for the Parallel use is down to the fact that it simply took too long to Unload each domain and running them in parallel should prove to be faster (appears to be even though there is only one thread that's doing each Unload?!).

Based on other posts, I can only guess that this is somehow down to the fact I am using a ThreadPool Thread to Unload the AppDomains. I just can't see how to avoid it?

public partial class SomeService : ServiceBase
{
    private Manager _appDomainManager;

    protected override void OnStop()
    {
        _appDomainManager.Dispose();
    }
}

public class Manager : IDisposable
{
    public void Dispose()
    {
        Log.Debug("Disposing");
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed) return;
        if (disposing)
        {
            // dispose managed resources
            Parallel.For(0, appdomains.Length, UnloadAppDomian);
        }

        _disposed = true;
    }
}

private UnloadAppDomain(int appDomainIndex);

public static class Parallel35
{
    public static void For(int start, int end, Action<int> action)
    {
        var waitHandles = new WaitHandle[end - start];
        for (int j = 0; j < waitHandles.Length; j++)
        {
            waitHandles[j] = new ManualResetEvent(false);
        }

        for (int i = start; i < end; i++)
        {
            int i1 = i - start;
            ThreadPool.QueueUserWorkItem(
                state =>
                {
                    try
                    {
                        action((int) state);
                    }
                    finally
                    {
                        ((ManualResetEvent) waitHandles[i1]).Set();
                    }
                }, i);
        }
        WaitHandle.WaitAll(waitHandles);
    }
}

解决方案

I tracked down this as a bug to one of the AppDomains on exit waiting for a WaitHandle that's never set.

If a thread does not abort, for example because it is executing unmanaged code, or because it is executing a finally block, then after a period of time a CannotUnloadAppDomainException is thrown in the thread that originally called Unload.

The AppDomain now unloads relatively quickly and my service stops quite quickly.

这篇关于而错误的AppDomain卸载。 (异常来自HRESULT:0x80131015),Windows服务里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆