I’ve a few web methods that I use to call some external services like the Google Calendar API, obviously these can be extremely brittle.
Unfortunately I now realise that any error thrown on these methods are not causing an exception to bubble up to Global.asax which is where errors are getting logged in this application.
I have seen suggestions to wrap the method in a try/catch, which is a stupid way of doing it as there are a variety of errors that ASP.Net will silently swallow still.
In trying to find a solution I’ve seen a lot of references to SoapExtension, which is exactly what I want to do but doesn’t get fired as I’m returning Json. What I really want is a way to catch the error just like that.
Any pointers appreciated, I still can’t understand how the ASP.Net team could have thought that silently swallowing errors like this was a bright idea.
So for example a method like this:
[WebMethod]
[ExceptionHandling] //can I write a handler like this to catch exceptions from JSON webservices?
static public void DeleteItem(string id)
{
var api = new GoogleCalendarAPI(User.InternalUser());
api.DeleteEvent(id);
return "success";
}
There is no equivalent to
SoapExtensionfor JSON WebMethods and having custom errors turned on in your production site will result in a generic error message being returned to the client, no error is ever raised on the server. You cannot circumvent this.If you inspect the code using something like ILSpy, there is no way to pass a method or class to page WebMethods like
SoapExtension. The error is swallowed by ASP.Net as it invokes the web method, the only notification you will get is a HTTP 500 error sent to the client with a total generic error message.In 4.0, WebMethods get called by this:
So if invoking your method throws an error it will call the following code with a statusCode of 500, there’s no re-throw in there and nothing else you can pass in called so unless I’m being blind it just gets swallowed silently. Even worse if you’ve got custom errors turned on, which any sane person will, it’ll completely obfuscate the original cause:
I can’t see a way around it, looks like WebMethod is not ready for production code, shame.