处理来自服务中产生的线程的异常 [英] Handling exceptions from spawned threads in a service

查看:119
本文介绍了处理来自服务中产生的线程的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我有一个使用Activator.GetObject实例化对象的服务,该对象的类型直到运行时才能知道.我们提前知道的是它实现了适当的接口. (组装和类型标识信息来自其他接收到的数据.)新对象连接到某个外部设备(串行端口,TCP套接字等),并处理与之的通信.

问题:

如果在该对象的线程之一中未处理异常,我该在哪里以及如何在服务中捕获该异常?

我已经在服务对象,执行实例化的对象以及对象本身中尝试了AppDomain.CurrentDomain.UnhandledException的处理程序,但是这些都不会捕获为测试注入的NullReferenceException. (在检索实例化对象从计时器事件访问的对象的属性之一时,我就抛出了它.)还有哪些其他选项?

更多发现:如果将脚本放置在Main()ServiceBase对象的构造函数中,则UnhandledException订阅适用于在调用ServiceBase.Run()之前发生的异常.但是,这两种(通常是微不足道的)方法是在该空间中发生的唯一事情.服务启动后,无论在何处引发异常,任何订阅都不会在任何地方进行工作-即使在订阅之后的下一个语句中也是如此.

The Scenario:

I have a service that instantiates an object using Activator.GetObject whose type cannot be known until runtime. All we know ahead of time is that it implements the appropriate interface. (Assembly and Type identification information comes from other received data.) The new object connects to some external device (serial port, TCP socket, whatever) and handles communication with it.

The Problem:

If an exception goes unhandled in one of the threads of that object, where and how can I catch it in my service?

I have tried a handler for AppDomain.CurrentDomain.UnhandledException in the service object, the object that performs the instantiation, and in the object itself, but none of these will catch the NullReferenceException that I injected for my testing. (I had it thrown upon retrieving one of the object''s properties that the instantiating object accesses from a timer event.) What other options are there?

Further discoveries: The UnhandledException subscription works for exceptions that happen before the call to ServiceBase.Run() if the scbscription is placed in Main() or the ServiceBase object constructor. But those two (usually trivial) methods are the only things that happen in that space. Once the service is being Started, no subscription works anywhere regardless of where the exception is thrown--even in the very next statement after the subscription.

推荐答案

尝试使用Google搜索"Windows服务未处理的异常".在我获得的点击量中,
Try Googling ''windows service unhandled exception''. Of the hits I got, the first answer in http://webcache.googleusercontent.com/search?q=cache:KnK7iUFzTHEJ:stackoverflow.com/questions/58280/unhandledexception-handler-in-a-net-windows-service+windows+service+unhandled+exception&cd=4&hl=en&ct=clnk&gl=us[^] appears to shed some light on why the AppDomain unhandled exception handler isn''t responding.


这篇关于处理来自服务中产生的线程的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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