.NET asmx error handling

  • Hey,

    I've been looking for ages to try to find a way to catch and log invalid requests to an ASMX webservice. By invalid requests, I mostly mean invalid SOAPAction values or inbound XML that doesn't validate against the WSDL. I can easily trap exceptions thrown from my own code once the SOAP handlers pass the commands down to my code, but I'm having absolutely no luck catching anything that goes wrong before it hands over that control.

    So far I've tried making a SoapExtension and using the ProcessMessage step and trying to catch the errors but it appears they're caught further up the tree, as all I get is an Initialize event then it falls over without ever reaching ProcessMessage. I've also tried a global.asax, and I at least got to log the entire request there, but the error event doesn't fire cause .NET figures the ASMX has already handled it by sending back a Soap Fault/Soap Exception. I've even tried reading the output stream in the finished request event and was figuring I'd parse the XML for soap errors then just log all the raw data, but I couldn't get it to read the outputstream (presumably cause it's already being sent).

    There's a part of me saying "It's not your code that has the error, it's theirs!", but the problem is when people can't work out how to use an API they're generally also too stupid to provide good debug info for us to find out what they're doing wrong. I've had to set it up before to just email me all raw input data from the asax that matched a set of rules to try to track down issues, and that just strikes me as horrible. Next stop is using reflection to trawl over the .NET assemblies to try to find somewhere where I can tie in or override functionality to get my hands on the error.

    Anyone already butted heads with this issue before? Is this just something you can't do in an ASMX or is there some silly little thing I'm missing?

  • I'm not really an expert on .Net 2.0 web services, but my first thought would be to write a HttpModule that plugs into IIS to handle all the exception logging.  You should have complete access to the request and response to do whatever you want.

    Another option would be to convert to a WCF based web service and use it's logging functionality to track down malformed messages.  

  • Yeah, the HttpModule approach was what I was aiming at with the global asax, but I couldn't seem to get my hands on the response stream. I did think it was a bit odd for there to be no way to do that though, and I found the Filter response parameter which allows you to make all the response data go through any Stream object, so I'm thinking maybe the answer is to just create one of those that buffers up the data and in the endrequest try to write a few simple commands for sniffing out things that are indicative of errors in the data.

    It does seem sort of messed up that you'd have to do that given how well most of .NET's error handling hooks in, and doesn't really seem like a viable long-term solution cause it means adding a second layer of parsers to everything (Was hoping for something I could viably run on a live environment without a big performance impact).

    As far as WCF based services go, I've been thinking of looking at them for future services but there's a lot of existing clients integrated against the asmx services and a lot of services to convert so without a serious benefit I doubt it'll happen. But for future ones it might help so I'll read into what error handling those have, thanks for the tip.

  • I realize this is a month old post, but in case anyone else needs an answer...

    Jeff Atwood wrote a pretty good piece on Code Project quite a few years ago about error handling, which included how to handle web service errors.


  • Hey, I tried that exact method actually, but the problem is that'll only catch an error thrown by your handler code. If the inbound request is invalid (SoapAction not supported or the XML not matching the WSDL's schema) the error handling on that codeproject page .

    I've basically wound up with a monstrosity that sniffs all the ASMX traffic and scans the XML for things that look like errors and forwards the raw XML in email if it finds one, which to me sounds more like a sidebar solution than a real error handling, so if anyone does know a good answer I'm still all ears even months after cause I haven't found a good answer 😛

    PS: There's this new invention called domain names... 😛

  •  I think you need a SoapExtension. You can intercept SOAP request before and after deserialization.

Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.