在 Windows 服务中运行的线程中不执行 finally [英] Finally is not executed when in a Thread running in a Windows Service

查看:33
本文介绍了在 Windows 服务中运行的线程中不执行 finally的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释为什么不执行这个 finally 块?我已经阅读了有关何时不应执行 finally 块的帖子,但这似乎是另一种情况.此代码需要 TopShelf 和 log4net.我正在运行 .net 4.5

Can anyone explain why this finally block is not executed? I have read posts about when to expect finally block not be executed, but this seems to be another case. This code needs TopShelf and log4net. I am running .net 4.5

我想一定是 Windows 服务引擎启动了未处理的异常,但为什么它在 finally 块完成之前运行?

I guess it must be the Windows Service engine that kicks in on unhandled exceptions, but why is it running before the finally block has finished?

using log4net;
using log4net.Config;
using System;
using System.Threading;
using Topshelf;

namespace ConsoleApplication1
{
    public class HostMain
    {
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                x.Service<HostMain>(s =>
                {
                    s.ConstructUsing(name => new HostMain());
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });

                x.RunAsLocalSystem();
                x.SetServiceName("TimerTest");
            });
        }

        public void Stop()
        {
            LogManager.GetLogger("MyLog").Info("stopping");
        }

        public void Start()
        {
            XmlConfigurator.Configure();

            LogManager.GetLogger("MyLog").Info("starting");

            new Thread(StartServiceCode).Start();
        }

        public void StartServiceCode()
        {
            try
            {
                LogManager.GetLogger("MyLog").Info("throwing");

                throw new ApplicationException();
            }
            finally
            {
                LogManager.GetLogger("MyLog").Info("finally");
            }
        }
    }
}

输出

starting
throwing
stopping

请评论您降级的原因,也许您不明白问题所在?我在这里看到一个大问题.您编写了一些域逻辑,这些逻辑在有关异常的 finally 子句中执行重要操作.然后,如果您将逻辑托管在 Windows 服务中,设计就会突然中断.

Please comment why you are downgrading, maybe you don't understand the problem? I see a big problem here. You write some domain logic that does important stuff in the finally clause on Exception. Then if you host the logic in a Windows Service the design suddenly is broken.

推荐答案

来自 MDSN try-finally (C#参考)

在处理的异常中,关联的finally 块保证运行.但是,如果异常未处理,finally 块的执行取决于如何触发异常展开操作.反过来,这取决于您的计算机的设置方式.有关详细信息,请参阅CLR 中未处理的异常处理.

Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.

通常,当一个未处理的异常结束应用程序时,是否运行finally块并不重要

这是设计使然,.NET 选择终止您的应用程序,原因是,出现了严重错误,某些事情没有按预期工作,通过调用 finally,我们不想造成更多损害,所以最好是结束应用程序.

This is by design, .NET has chosen to terminate your application, reason is, there is something terribly wrong, something didn't work as expected, by calling finally, we don't want to do more damage, so best is to end the application.

如果最后再抛出一个异常怎么办?如果应用程序即将关闭,它可能已经关闭或开始关闭托管资源,并且访问它们以最终登录也可能是致命的.

What if finally throws one more exception, where does that go? If application is about to close, it may have closed or started closing managed resources and accessing them for logging in finally could turn out to be fatal as well.

这篇关于在 Windows 服务中运行的线程中不执行 finally的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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