Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I keep getting WCF service is in faulted state on the client side. How should I catch WCF exceptions, without breaking my WCF service?

Tags:

c#

asp.net

wcf

I have a WCF service multiple UIs consume. When the user is unable to access the database, I get Unauthorised exception from the DB. I don't catch the exception on the server side and send it to the client.

On the client (asp.net webpage), I get to see the exception - User was unable to access the database, login failed. This is all good. But... if I make a call to the WCF service again, I get the exception that the service is in faulted state. Only open is to restart the entire WCF service. WCF service is hosted as a windows service.

What is the best way to catch the exceptions, log the exception on server side, send the exception details back to the client without breaking the service? Thanks

like image 845
InfoLearner Avatar asked Nov 18 '11 11:11

InfoLearner


1 Answers

Unhandled exceptions will fault the communication channel. As others have pointed out, a faulted channel must be aborted by calling Abort() - it can't be closed and it can't be used again.

To address the other part of your question, the "best way to catch the exceptions, log the exception on server side, send the exception details back to the client without breaking the service?", you need to use FaultException Class.

Additionally, you can use the IErrorHandler Interface to wire up your service host to catch any exceptions that were not otherwise caught (i.e., a global error handler). There are many examples of how to do this on the net - just google for WCF IErrorHandler.

Here's a couple:

WCF Exception Handling with IErrorHandler

WCF Extensibility – IErrorHandler

like image 92
Tim Avatar answered Oct 13 '22 23:10

Tim