2

I have a situation where I use jQuery to call Webmethod in .aspx codebehind (not asmx webservice or anything similar). I have a simple error logger defined in Global.asax ApplicationError, so (almost) all exceptions are being caught and logged in a single place. The thing here is that ApplicationError method is not reached if an exception occurs inside a webmethod.

Here's a simple example:

Webmethod call:

$.ajax({
            type: "POST",
            url: "/MyMethods.aspx/MyMethod",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert('ok');
            }
        });

Webmethod implementation:

Public Class MyMethods
    Inherits System.Web.UI.Page

    <System.Web.Services.WebMethod()> _
    Public Shared Function MyMethod() As String
        Throw New Exception("Forced.")

        Return sb.ToString
    End Function
End Class

And global.asax

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim filename As String = "d:\test.txt"

        Using objWriter As New System.IO.StreamWriter(filename, True)
            objWriter.WriteLine(Server.GetLastError().GetBaseException().Message)
        End Using

End Sub

Is there any way to 'convince' ApplicationError method to be reached when webmethod throws an exception? I have tried (literally) hundreds of scenarios:

...but none of them worked so far. So any help would be strongly appreciated!

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
Spartak
  • 61
  • 6
  • The second post looks to have a viable approach in some respects; however, what happens if you handle UnhandledException in Application_OnStart? i.e. put AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); in Application_OnStart with the UnhandledExceptionHandler in your Global.asax too? – dash Mar 06 '12 at 17:39
  • Thank you for the reply, but unfortunately this doesn't seem to work as well... I mean, it logs any other errors, but not the webmethod ones. I put a breakpoint on the handler code and it wasn't hit. Also, the txt log remains empty. – Spartak Mar 06 '12 at 18:14
  • Possibly a silly question, but how do you know you are getting an exception? Are you getting an error message in a different way? – dash Mar 06 '12 at 22:16
  • Yes, VisualStudio lets me know about unhandled exception inside webmethod (for testing purposes I even force throw an exception, as described above). And if I put the breakpoint at the beginning of a webmethod and do step-by-step execution I can confirm that exception is thrown. Is it possible that VS itself terminates the current execution flow when the exception happens? As far as I know, it shouldn't... – Spartak Mar 07 '12 at 13:25
  • My fault for not paying closer attention - sorry! You are talking about Page Methods. This has already been answered on SO - see http://stackoverflow.com/questions/785585/server-side-asp-net-ajax-exception-handling – dash Mar 07 '12 at 14:19

0 Answers0