views:

139

answers:

2

Hello,

I have developed a small WCF service which handles HTTP request.

I want to be aware of every fault that happens:

  • everything that causes 500 Internal Server Error from the contracts view
  • everything that causes a CommunicationException from the bindings view (I've written a custom one but am using standard ones also)

The errors should be put into the windows event log.

My question is:

  • Is there some possibility to add a central event handler for the whole WCF stack which captures all Exceptions and prints them into the event log?

Greetings.

+1  A: 

You can use the IErrorHandler interface for this purpose.

John Saunders
+1  A: 

On the server side - yes, as John already pointed out, you can basically implement the IErrorHandler interface on your service class and capture all errors on that side.

However, on the client side, you'll just have to use good old .NET try {...} catch {...} to secure all service calls - after all, you could potentially

  • call a server which doesn't exist at all
  • call a server which is temporarily unavailable
  • loose network connectitivity (guy with a big bulldozer cut your network wire :-) )
  • more of these kind of things

Those would be CommunicationExceptions (or descendants thereof) and must be handled on the client side as they happen.

So you can handle the server side of things centrally (at least as long as the message has gotten through to your service class and is being processed) - anything else must be handled separately.

Marc

marc_s
I've inserted a "throw new CommunicationException("error happened")" into the ReceiveRequest method of the custom binding's channel. The custom error handler was inserted via "foreach (ChannelDispatcher cd in h.ChannelDispatchers) { cd.ErrorHandlers.Add(new MyErrorHandler()); } However, my custom error handler doesn't get called at any time.
Etan
I think you're throwing too early! :-) If I remember correctly, the IErrorHandler interface only kicks in once the dispatcher on the server side has dispatched the message to a service object's method. Anything before that will be sent back straight to the client
marc_s
putting the "throw new CommunicationException("error")" into the contract's implementation won't work also. What exceptions are handled by the errorhandler if not those before the dispatch and not those after the dispatch?
Etan
IErrorHandler is intended for "normal" service implementers, and it handles all errors that occur during execution of the service method (which typically are 99% of all errors). I
marc_s
Am I right that "service implementer" is the class which implements the contract? For example if I have an ICalculator contract with a Divide function, the error handler should be invoked when a client tries to divide by zero over an implementation of this contract? If yes, why doesn't it get invoke if you just add "throw new Exception("error")" to such a function's body? For the ServiceModel, it should be the same, or are my thoughts wrong?
Etan
@Etan: yes, exactly - the class that implements the service contract and thus provides the actual functionality for the service, that's what I mean
marc_s