Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: server-side error handling. best practice

Tags:

.net

wcf

I write WCF service and want to keep it's activity log: trace, warnings and error messages. The very straight-forward approach is to wrap all contract service code inside try/catch sections and write error messages from catch part rethrowing service contract error exceptions.

I suppose it would be nice if there would be one code-point to catch all uncaught exceptions instead of dozens try/catch sections. It would be also useful to write contract method parameters from that point to provide detailed error information.

Is it possible with WCF?

What is the best practice in organizing error handling inside WCF services?

Thank you in advance!

like image 296
Andrew Florko Avatar asked Jul 22 '10 12:07

Andrew Florko


People also ask

Which type of exception can be thrown from WCF service?

Fault exceptions are exceptions that are thrown by a WCF service when an exception occurs at runtime -- such exceptions are typically used to transmit untyped fault data to the service consumers.

Which type of behavior should be configured to return exception detail from WCF service?

We can use FaultException &/or FaultContract (in System. ServiceModel namespace) to these exception details to the client.

Which property must be set in WCF configuration to allow users to see exception Details of WCF?

It is mandatory to provide the typeof property with FaultContract. Also, if no fault contract attribute is applied, the default exception that will be returned by WCF will be of type FaultException. Run the WCF test client and invoke the method. We can see the details of the custom message that we set.


2 Answers

Have a look at IErrorHandler interface

It allows an implementer to control the fault message returned to the caller and optionally perform custom error processing such as logging

You can implement your service behavior (see IServiceBehavior interface) and add your handler to ChannelDispatcher error handlers.

See WCF Exception Handling with IErrorHandler for more details.

like image 106
Incognito Avatar answered Oct 31 '22 17:10

Incognito


This article explains very well the different practices and the best one. Long but will probably answer your question.

(Check Ideal Scenario if feeling lazy)

Article URL: http://www.olegsych.com/2008/07/simplifying-wcf-using-exceptions-as-faults/

like image 20
Oscar Foley Avatar answered Oct 31 '22 18:10

Oscar Foley