.net – WCF: server-side error handling. best practice

netwcf

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!

Best Answer

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.

Related Topic