Strategies for Error Handling in .NET Web Services
I have a fairly substantial library of开发者_高级运维 web services built in .NET that I use as a data model for our company web sites. In most .NET applications I use the Global ASAX file for profiling, logging, and creating bug reports for all exceptions thrown by the application.
Global ASAX isn't available for web services so I'm curious as to what other strategies people have come up with to work around this limitation. Currently I just do something along these lines:
<WebMethod()> _
Public Function MyServiceMethod(ByVal code As Integer) As String
Try
Return processCode(code)
Catch ex As Exception
CustomExHandler(ex) 'call a custom function every time to log exceptions
Return errorObject
End Try
End Function
Anybody have a better way of doing things besides calling a function inside the Catch?
Assuming you can't take the advice of ladenedge, http://msdn.microsoft.com/en-us/library/ds492xtk(VS.71).aspx suggests there is no way to use the global error handling, which leave you stuck with your current method:
"A Web application can be comprised of multiple XML Web services, however the Application_Error event within the Global.asax file cannot be used for global exception handling. The HttpHandler for XML Web services consumes any exception that occurs while an XML Web service is executing and turns it into a SOAP fault prior to the Application_Error event is called."
Even if you have a ton of functions, at least you're dispatching the exceptions to a common place instead of using cut-and-paste programming. It looks like your method can fit the DRY principle. In other words, 'you're fine!'
精彩评论