Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF - how to make custom errors?

Tags:

c#

.net

wcf

I've got a WCF REST project and I want to catch the errors that the WCF framework throws and display them in my way (JSON, that is).

For example, If I'm expecting an int parameter in my call and I get a string, the framework would display a page with "request error" and some trace info... I'd just like to get the exception and display it in my JSON format as response to the user.

Just to make this more clear - I'm not looking to catch an exception IN CODE, but an exception that happens outside of the code. an exception that the WCF would generate it self, such as (when I passed a string into an int field)

The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service. The exception message is 'Input string was not in a correct format.'. See server logs for more details. The exception stack trace is:...

Any ideas how this can be done?

Many thanks in advance!

like image 711
Roman Avatar asked Feb 26 '26 06:02

Roman


1 Answers

You need to use FaultContracts to generate your own custom errors. Takes a bit of time to setup but once you have it, it works wonders. The good thing about it is that the FaultContract is completely serializable.

[OperationContract]
[FaultContract(typeof(CustomFault))]
string WillThrowArgumentException();

Here is a really good explanation of getting it to work.
http://blog.ngommans.ca/index.php?/archives/33-Handling-custom-errors-in-WCF.html

like image 77
Michael D. Irizarry Avatar answered Feb 27 '26 20:02

Michael D. Irizarry