.NET Web Service Exception Exception

I have a set of web services generated using the [WebMethod] attribute. Is it considered "good practice" to throw an ArgumentException from such a method if its arguments are not specified properly (and reasonable default values ​​cannot be used)? If so, should this exception be caught and re-thrown to register it both on the server and on the client?

+4
source share
3 answers

No, throwing exceptions from web services is not good practice, since .NET Exceptions (like ArgumentException ) does not support cross-platform (think about how the Java client should respond).

The standard mechanism for specifying exceptions in web services is Soap Error .

With .asmx throwing a SOAPException will throw an error.

If you go to WCF, you can look at FaultContracts .

To improve debugging of remote exceptions between the .Net client and the .Net server, you can spoof and send an exception through the wire using includeExceptionDetailInFaults in your config. The exception itself must be serializable for this to work. However, you will want to disable this before your system achieves performance.

On the sidelines, you will often find that if the calling SOAP request call of the caller is too poorly formed (for example, if your arguments include objects that cannot be deserialized) that your WebMethod will not be called at all - the caller will just get an error (often quite cryptic).

The above errors caused by invalid arguments to call the client to your service should be generated when checking the arguments to the call.

Perhaps related - after the request has passed the check, for your own approval of the state of the internal system, you can also use Code Contracts to detect internal errors (or, possibly, missing checks that should have happened earlier). However, they will not be transferred to the client.

+4
source

Using exceptions and custom error codes is always a difficult decision. You must evaluate how “exceptional” the case is and how you plan to handle the error on the consumer side. The exception is usually unforeseen cases (for example, an important parameter is missing) and custom error codes to handle business-related errors.

UPDATE: this, of course, is only applicable if you are creating a new service. If you modify an existing one, you should know how its existing clients use it and how they expect error codes.

+3
source

Here is a good article on web service exceptions

+3
source

Source: https://habr.com/ru/post/1339000/


All Articles