为什么 global.asax Application_Error 方法不能捕获 ASMX 服务抛出的异常?

发布于 2024-08-30 19:30:37 字数 34 浏览 0 评论 0 原文

以及如何解决它。我想记录每个抛出的异常以用于维护目的。

And how to fix it. I'd like to log every thrown exception for maintenance purpose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月棠 2024-09-06 19:30:37

这是 .Net 中的一个已知问题 - Web 服务永远不会触发 Application_Error。不确定是否有任何原因是设计使然,但它就是行不通。

杰夫·阿特伍德 (Jeff Atwood) 发表了一篇帖子(以及 follow-up) 几年前关于这个问题,有以下想法

  • :每个 Web 服务方法周围的 try-catch 块
  • 使用外观设计模式并将 try-catch 包含在父对象中
  • 编写自定义 SOAP 扩展或 HTTPModule

我唯一关心的是第一个,尽管它看起来像很多工作。

This is a known issue in .Net - Application_Error never fires for a web service. Not sure if there's any reason it would be by design, but it just doesn't work.

Jeff Atwood had a post (and follow-up) about this a few years ago, with the following ideas:

  • Put a try-catch block around each web service method
  • Use a facade design pattern and include the try-catch in parent objects
  • Write a custom SOAP extension or HTTPModule

The only one I care for is the first one, even though it seems like a lot of work.

他夏了夏天 2024-09-06 19:30:37

更好: HttpApplication.Error 事件

注意:

如果您的 Web 应用程序包含 XML
Web 服务,无法使用错误
全局异常处理事件
那些服务。 HTTP 处理程序
XML Web 服务消耗任何
XML Web 中发生的异常
服务并将其转换为 SOAP
调用 Error 之前发生的错误。
要处理 XML Web 服务异常,
构建 SOAP 扩展来处理 Web
自定义全局中的服务异常
异常处理程序。了解更多
信息,请参阅处理和抛出
XML Web 服务中的例外

Better: HttpApplication.Error event:

Note:

If your Web application contains XML
Web services, you cannot use the Error
event for global exception handling of
those services. The HTTP handler for
XML Web services consumes any
exception that occurs in an XML Web
service and converts it to a SOAP
fault before the Error being called.
To handle XML Web service exceptions,
build a SOAP extension to process Web
service exceptions in a custom global
exception handler. For more
information, see Handling and Throwing
Exceptions in XML Web Services
.

陌上芳菲 2024-09-06 19:30:37

对于任何寻找 Microsoft 关于此主题的信息的人,请参阅此处:“处理和抛出 XML Web 服务中的异常”。

这是相关部分:

Web 应用程序可以由多个 XML Web 服务组成,但是 Global.asax 文件中的 Application_Error 事件不能用于全局异常处理。 XML Web 服务的 HttpHandler 会处理 XML Web 服务执行时发生的任何异常,并在调用 Application_Error 事件之前将其转换为 SOAP 错误。

For anyone looking for Microsoft's word on this topic, see here: "Handling and Throwing Exceptions in XML Web Services".

This is the relevant section:

A Web application can be comprised of multiple XML Web services, however the Application_Error event within the Global.asax file cannot be used for global exception handling. The HttpHandler for XML Web services consumes any exception that occurs while an XML Web service is executing and turns it into a SOAP fault prior to the Application_Error event is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文