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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With