Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle low level WCF errors?

Tags:

wcf

I have the standard error handing in place in my service:

  • I have an IErrorHandler hooked to the service to handle unexpected errors during service execution.
  • I have try/catch blocks in all my service methods to handle expected cases.

However, there are cases where exceptions are thrown on the server and neither is called.

Here is a case where the server exception is not sent to the IErrorHandler:

  • Set the receiveTimout on the server binding to 5 seconds.

  • On the client do this:

.

Service1Client sc = new Service1Client();
ICommunicationObject o = sc as ICommunicationObject;

o.Open(); // open channel

sc.GetData(10); // do a first call

Thread.Sleep(10000); // wait longer than the server receiveTimeout

sc.GetData(10); // Attempt another call: server throws a FaulException

In that case, the error is thrown on the server but I cannot find a way to handle it (and log it). I know an error is raised because if I attach a debugger on the server process and break on all exceptions, the debugger breaks.

I have found other similar cases where low level errors are not passed to my program.

Where can I hook my code to ensure that I can handle ALL exceptions that occur on the server before they are returned to the client app? Should I implement my own IChannel or some other low level interface?

Thanks

UPDATE Sep 21 2009: See this thread on the Microsoft WCF Forum. I'll probably have to implement my own Channel if I want to handle this type of exception. I'll update this post again when I have more info.

like image 358
Sylvain Avatar asked Aug 13 '09 13:08

Sylvain


Video Answer


1 Answers

After much research and experimentation, the answer is:

At this time (.Net 3.5) there is no mechanism that allows one to handle all possible exceptions that may occur in the context of a WCF call.

Exceptions that happen during the service method execution can easily be handled with:

  1. Try/catch blocks in all service methods to handle expected cases.
  2. IErrorHandler hooked to the services to handle unexpected errors during service execution.

However, for low level WCF infrastructure errors, there is no perfect solution. The best solution that exists seems to be to implement a custom channel to catch more exceptions.

In this Microsoft Connect Bug Report, Microsoft confirms that there is no way to handle all types WCF infrastructure errors.

In this thread on the Microsoft WCF forums, there is a sample on how to implement a custom channel. That solution only works for HTTP, not for HTTPS. Also some WCF infrastructure errors are not caught by the custom channel either (see more details in that specific thread).

like image 186
Sylvain Avatar answered Oct 02 '22 14:10

Sylvain