【问题标题】:ASP.NET Web service ignoring Application_Error and UnhandledException eventASP.NET Web 服务忽略 Application_Error 和 UnhandledException 事件
【发布时间】:2016-05-01 00:15:28
【问题描述】:

我的网络服务中有一个被忽略的异常事件的小问题。

这是我的 Global.asax.cs 文件:

using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace AcmeDemoApp
{
    public class Global : System.Web.HttpApplication
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected void Application_Start(object sender, EventArgs e)
        {
            logger.Debug("Application_Start");

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            logger.Debug("Session_Start");
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            logger.Debug("Application_BeginRequest");
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            logger.Debug("Application_AuthenticateRequest");
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            logger.Debug("Application_Error");
        }

        protected void Session_End(object sender, EventArgs e)
        {
            logger.Debug("Session_End");
        }

        protected void Application_End(object sender, EventArgs e)
        {
            logger.Debug("Application_End");
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            logger.Debug("CurrentDomain_UnhandledException");
        }
    }
}

在我执行请求之后。我从网络服务收到此错误。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <soap:Fault>
            <soap:Code>
                <soap:Value>soap:Receiver</soap:Value>
            </soap:Code>
            <soap:Reason>
                <soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.ArgumentException: Error
   --- End of inner exception stack trace ---</soap:Text>
            </soap:Reason>
            <soap:Detail />
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

在 NLog 的日志文件中我只看到:

[2016-05-01 02:11:25.3629] [Info] Application_BeginRequest 
[2016-05-01 02:11:27.3861] [Info] Application_AuthenticateRequest 

为什么会忽略 UnhandledException? 我还必须使用其他事件或方法来获取所有soap 错误吗?

或者你能告诉我如何解决这个问题吗?

提前感谢您的任何想法!

【问题讨论】:

  • 尝试处理Application.ThreadException
  • 没有@VladL 我没有Application.ThreadExceptionApplication 存在但没有 ThreadException 事件/属性。

标签: c# asp.net nlog global-asax unhandled-exception


【解决方案1】:

使用 UnhandledExceptionHandler。

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

【讨论】:

  • 我在 [WebMethod] 的第一行尝试了这个,当触发未处理的异常时,函数(处理程序)没有被调用。
【解决方案2】:

一个 Web 应用程序可以由多个 Web 服务组成。 但是,Global.asax 语法文件中的 Application_Error 事件不能用于全局异常处理。 Web 服务的 HttpHandler 使用 Web 服务执行时发生的任何异常并将其转换为 SOAP 错误在调用 Application_Error 事件之前。构建 SOAP 扩展以在全局异常处理程序中处理 Web 服务异常。 SOAP 扩展可以检查 ProcessMessage 方法中是否存在异常。

Handling and Throwing Exceptions in XML Web Services

同一主题的另一个讨论:Why global.asax Application_Error method does not catch exceptions thrown by ASMX service?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-19
    • 2014-12-30
    • 2020-05-23
    • 1970-01-01
    • 2017-09-05
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多