Custom exceptions with error codes in .NET Web Services to Client
I'm trying to throw custom exception from a web service and trap it on the client side. I'm using JQuery and getting the JSON-serialized exceptions, so that's a good start. H开发者_StackOverflowowever the problems are:
- Stack trace is sent to the client which i don't want.
- I need to add an error code in addition to the exception message.
I've tried creating a custom exception, but it's omitting the error code when serializing.
To send your own data back with a SoapException, you need to set the Details property on the SoapException object. Sorry I don't have my source code in front of me, but I believe you have to pass the details as a string to the SoapException object. I subclassed the SoapException object, passing its constructor the details I want. It then calls the SoapException constructor passsing the details along with some standard stuff I always want returned (it check the .Data property of the exception and adds it to the details). It turn all the stuff into XML for the details. On the client side I check for XML in the fault details and then create a new JavaScript error object and add the details as properties, then throw it.
So pass the error code in the details to the SoapException constructor. Custom exception properties are ignores by SoapException.
To turn off the stack trace I belive you fiddle with web.config.
精彩评论