Introduction
Years ago, I personally met the famous error "System.ServiceModel.FaultException'1 was not handle by user code" in one of my WCF project. Since that tile I get a lot of great comment on my blog on that same issue still today. In order to bring to that topic a better visibility, I decided to write and update that article here on this wiki.
Managing a correct error handling process in an application is a nightmare if you want to do it properly and I would say that it is not always the best exciting part, but it is absolutly needed to prevent as much as possible unhandle situation. In a normal application you simply place the code inside try-catch block with the exception type, and there are handle as normal .net exception object that you can bubble up.
The FaultContract
OperationContract]
[FaultContract (
typeof
(MyError))]
Boolean myMethod();
MyError is here a custom type which define an error message
Server side code:
catch
(Exception exc)
{
MyError ErrLog =
new
Maillefer.Nomos.Types.Common.MyError (“This
is
an error”,”Critical”);
FaultException<MyError> fe =
new
FaultException<MyError >(ErrLog,
new
FaultReason(ErrLog.Message));
throw
fe;
}
Client side code:
From the client application side you simply need to catch the FaultException error type as you normally do and retrive the message return by the Servcie SOAP message as follow:
catch
(FaultException <MyError> MyError)
{
string
msg = Error.Detail.Message;
MessageBox.Show (msg);
wcfclient.Close();
}
You then receive the error message send from your server, inside your client application.. Hmmm this is what I was expecting but it was not behaving as expected. I spend days to cross check my code and verify impementation to get the exception correctly thrown but when runnig my application my service was stoping at the time it was throwing the exception (throw fe). The error return from that execution was something strange like :
“System.ServiceModel.FaultException`1 was unhandled by user code“
After a lot of research I find out the solution on a post mentionning that it was due to some setting of debugging scenario of my VS environement, which make my code execution stop at each exception with not really logic message wich was giving a lot of confusion.
Disabling the error
To remove this behaviour it was advise to uncheck the 2 following options from theVisual Studio Editor Tools->Option menu of IDE:
So simple thing which could save your weeks.