如何在Windows Azure(Worker)角色中捕获未处理的异常

编程入门 行业动态 更新时间:2024-10-23 19:21:08
本文介绍了如何在Windows Azure(Worker)角色中捕获未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试捕获我的worker角色中的所有未处理的异常。我尝试将尝试 - catch block into Run()方法(如建议的 here )但没有成功。

public override void Run() { try { base.Run(); } catch(Exception ex) { Trace.TraceError(未处理的异常:{0},ex); throw ex; } }

角色承载一个WCF服务,所以没有 Run()方法中的其他逻辑。是否有另一种可能性在此级别捕获异常?

更新1 澄清问题:角色自己承载一个WCF service(初始化为 OnStart())其中一些操作是后台操作。当服务被调用并且该方法引发意外的异常时,我喜欢将其写入日志。

解决方案: 显然这就像在正常的C#应用​​程序中:只需在 UnhandledException 事件中添加一个处理程序,就像这样

AppDomain.CurrentDomain.UnhandledException + = new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);角色中的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 。我非常专注于Azure,所以我以为这根本就不行,甚至没有尝试过: - )

解决方案

在我的问题中已经更新了,为了完整性,我们回答:

显然这就像一个正常的c#应用程序:只需添加一个处理程序到 UnhandledException 这样的事件

AppDomain.CurrentDomain.UnhandledException + = 新的UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);角色中的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 。

I'm trying to catch all unhandled exceptions in my worker role. I tried putting a try-catch block into the Run() method (as suggested here) but with no success.

public override void Run() { try { base.Run(); } catch (Exception ex) { Trace.TraceError("Unhandled Exception: {0}", ex); throw ex; } }

The role hosts a WCF service, so there is no other logic inside the Run() method. Is there another possibility to catch exceptions at this level?

Update 1 To clarify the problem: The role self hosts a WCF service (initialized in OnStart()) where some operations are background operations. When the service is called and that method throws an unexpected exception, I like to catch that to write it to the log.

Solution: Obviously it is like in a normal C# application: Just add a handler to the UnhandledException event like this

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

inside the OnStart() of the Role. I was so focused on Azure that I supposed this couldn't work after all, that I didn't even try it :-)

解决方案

As already updated in my question, here for completeness's sake as answer:

Obviously it is like in a normal c# application: Just add a handler to the UnhandledException event like this

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

inside the OnStart() of the Role.

更多推荐

如何在Windows Azure(Worker)角色中捕获未处理的异常

本文发布于:2023-11-17 11:56:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   角色   未处理   如何在   Worker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!