0

I have a small problem with an ignored exception event in my web service.

This is my Global.asax.cs file:

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");
        }
    }
}

And after I execute a request. I get this error from the web service.

<?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>

And in the log file of NLog I see only:

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

Why is the UnhandledException ignored? Is there another event or method I must use to get all soap errors, too?

Or can you give me a hint how to fix this problem?

Thank you in Advance for any idea!

Patrick
  • 829
  • 2
  • 13
  • 34

2 Answers2

0

Use UnhandledExceptionHandler.

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
JAZ
  • 1,050
  • 6
  • 15
  • I tried this on the first line of a [WebMethod] and it the function (handler) was not invoked when an unhandled exception was triggered. – mike Mar 13 '19 at 05:01
0

A Web application can be comprised of multiple Web services. However, the Application_Error event within the Global.asax Syntax file cannot be used for global exception handling. The HttpHandler for Web services consumes any exception that occurs while a Web service is executing and turns it into a SOAP fault before the Application_Error event is called. Build a SOAP extension to process Web service exceptions in a global exception handler. A SOAP extension can check for the existence of an exception in the ProcessMessage method.

Handling and Throwing Exceptions in XML Web Services

Another discussion on the same topic: Why global.asax Application_Error method does not catch exceptions thrown by ASMX service?