Home > WCF > System.ServiceModel.FaultException`1 was unhandled by user code

System.ServiceModel.FaultException`1 was unhandled by user code

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.
With WCF service I have discover around different reading that it is a bit different, simply because WCF need to guaranty interoperability with any client application in order that they are able to catch error returned.
For that WCF need to convert the “normal .NEt exception” into a SOAP message exception that will be understandable from client application.
To achive this you need to specify a FaultContract attrribute in your service either declaratively or imperatively way. For my case I have done it with declarative.
You specify the declarative FaultContrat as follow for your service method:
  [OperationContract] [FaultContract (typeof(MyError))] Boolean myMethod();
MyError is here a custom type which define an error message
After having configured the service FaultContractAttribute, next you need to raise the exception from your server side service method code as follow :
Server side
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; }

So far so good. Then 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:

Client side

catch (FaultException MyError) {   string msg = Error.Detail.Message;    MessageBox.Show (msg);   wcfclient.Close(); }

And that’s it. 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.

To remove this behaviour it was advise to uncheck the 2 following options from the Tools->Option menu of IDE:


 Hoping this post will save you a lot of research time. Once again, every thing works until you hit the point that gives you nightmare 🙂

Categories: WCF Tags:
  1. Sam Martinez
    September 9, 2009 at 4:18 pm

    You’ve Got to be Kidding Me… That’s it?!?

    I, too, have spent days trying to figure out what was wrong with my code. I would reach the point in my service where I said Throw New FaultException… and it would then move to the next line of code and try to process it. I tried to find a way to actually get it to THROW and stop execution, but nothing worked, because I got the infamaous FaultException’1 message. But once I changed the options you mentioned in this post and reran the code, it worked just like it should and the error was captured in the client.

    Thank you so much for posting this!!

    • scalderara
      September 10, 2009 at 12:13 pm

      No problem Sam, I fight so long time to find it I though it would be more than helpful 🙂

  2. Nutan
    October 6, 2009 at 1:34 pm


    Thanks for you post.

    I did made the change in settings under Tools->Options, but now instead of throwing error it just passes the error generating steps silently.

    Any clue?

    • scalderara
      October 6, 2009 at 2:48 pm

      Are you cathcing this fault exception at the client side ?
      This tips was locking teh exception to be escaladate to client side.

  3. Ravi.S
    November 16, 2009 at 12:07 pm

    Hi ,

    Thanks for you post.this Helped me

  4. Hamish
    January 5, 2010 at 9:52 pm

    Sensational, I have been going over code for hours trying to figure out what I was doing wrong, had the same issue in a vb.net wcf solution…

  5. C# Developer
    January 18, 2010 at 10:11 am

    Thank you so much…very very useful.

    Cheers Mate

  6. Allan Rwakatungu
    February 7, 2010 at 3:45 pm

    Same here.
    Got this error using the enterprise library validation block and WCF …after hours of head banging ..this post saved the day

  7. Narayan Sahu
    February 9, 2010 at 2:52 pm

    Thanks a Lot Dear,It works wonder

  8. Miel Bronneberg
    April 21, 2010 at 3:38 pm

    You can also go to Debug -> Exceptions -> Common Language Runtime Exceptions, and uncheck “User-unhandled” for System.ServiceModel.FaultException`1. That way, only that specific type of unhandled exception will not cause a break.

  9. Scott
    July 13, 2010 at 9:54 am

    very strange that those options would have any impact on this… neither control what exceptions are handled…
    I have encountered this problem and noted that a try/catch around the ‘end’ method catches the fault whereas the try/catch around the calling and/or async method does not….

  10. Rajeev
    August 6, 2010 at 12:19 pm

    Thanx Buddy….It worked.

    But again I stucked to other error..
    ie- Cannot directly jumped to Server.Debugger is already attached.
    You can help me out….????

    • scalderara
      August 6, 2010 at 8:40 pm

      what do you mean by that, could you explain more

  11. Olivier Mayo
    August 6, 2010 at 5:37 pm

    Thank you soooooooo much! I’ve been searching for DAYS for this problem! I thought it was something in my code, and I couldn’t believe RIA services did not enable exception handling on the client side o_O

    You made my day!! What do I say? You made my week!!! 🙂 🙂

    Thanks a lot!

    • scalderara
      August 6, 2010 at 8:39 pm

      mais de rien Olivier ce fut un plaisir

  12. David White
    October 19, 2010 at 3:05 am

    Thank you!! This worked perfect… =) =)

  13. Alegntaye
    October 28, 2010 at 9:33 am

    Thank you very much. It was a lot of help

  14. Durga.V
    December 14, 2010 at 7:07 am

    I dont find much difference intially it was throwing the error as exception, after these changes it showing the same one in stackTrace as “The creator of this fault do not specify a reason” in Reference.cs. Help me for the same.

    • scalderara
      December 29, 2010 at 8:47 pm

      Try to add in your FaultException definition the New Faultreason as a parameter and specify corrcet attributes.

  15. Jonas
    December 29, 2010 at 12:11 pm

    Thanks for posting this. I have no idea what it actually does, but turning the two debugging options off, did the trick for me.

    Great job.

  16. abbu
    April 14, 2011 at 12:31 pm

    Thanx alot. it solved my problem..

  17. Adam
    April 14, 2011 at 1:56 pm

    Hi Thank you so verry mutch. Your information saved me many hours debuging.
    Keep ut the good work 🙂


  18. Lapierre
    May 3, 2011 at 2:12 pm

    Thanks a lot, Your information saved me many hours debuging. And i’m not sure, i found the solution.

  19. Bob
    May 12, 2011 at 4:09 pm

    Thanks man! I am sure I would have spent days on that one too had you not posted this.

  20. Ian Scott
    June 2, 2011 at 4:03 am

    Serge – Where do I send the cheque ? I have spent 4 days trying to determine why I could not sucessfully throw an exception from a WCF service.
    Thank you.

  21. jk
    June 9, 2011 at 8:02 pm

    i can only say thanks so much =D you save me from a lot of time waste

  22. Krishna
    June 22, 2011 at 4:19 pm

    Thanks dost, i got your post after a long Google search :). thanks again for this help 🙂

  23. August 18, 2011 at 9:16 am

    I am facing the same problem as my WCF is throwing “” when i throw an exception, after doing the desired changes in Tools–>Options–>Debuggging…, Its now executing the code as desired (NOT :-()

    Well, i am using a Silverlight 4 application client and it calls the wcf operations and Async calls. Now after the Fault is thrown from WCF, my code does not execute Catch block at all.

    Anyways, i did this;

    if (e.Error != null)

    but to my surprise, the fault is not what i threw. in fact its not even going to catch. Its saying “The remote server returned error : NotFound”.

    Please suggest….

    • scalderara
      August 25, 2011 at 10:57 pm

      could you psot a sample code which reproduce your problem ?

    • Sam Martinez
      August 26, 2011 at 11:10 am

      Silverlight 4 is highly prone to Cross-Domain Policy issues. I recently had troubles with my service NOT being found from Silverlight. I’d go to http://localhost:9091 and see that the service was running. Then I was able to successfully run the WCF test client, but everytime Silverlight complained about not finding my service.

      In fact it’s just being blocked. I had people tell me “you shouldn’t need a cross-domain policy since your service and client are on the same network”… BS! My service and client, in fact, were on the same localhost, but with different port numbers, and it would NOT work without a cross-domain policy or a client-access-policy file.

      What’s worse is that I had to find a way to provide a cross-domain policy in a self-hosted WCF service; I wasn’t using IIS. In the end, I found a solution that essentially creates another service end point in my WCF self-hosted service which provides a clientaccesspolicy.xml file when you type in http://localhost:9091/clientaccesspolicy.xml. Once I can type that in a browser and get the XML file, then my Silverlight client was finally able to use my WCF service.

      • scalderara
        August 26, 2011 at 12:39 pm

        Have you try to implement your service call with RIA to see if you get same issue?

  24. Steven
    August 29, 2011 at 2:02 pm

    Thanks a lot! This really helped me, because I’ve spend much time and nerves getting this to work. In VC#2010 Express you need to untick just one option:

    • Steven
      August 29, 2011 at 2:10 pm

      Enable Just My Code

  25. sagar
    March 2, 2012 at 10:50 am

    Thanks for posting this. It did save my time..

    • Serge
      March 7, 2012 at 6:07 am

      you welcome

  26. Suji
    March 20, 2012 at 9:19 am

    Thanks. Really helped!

  27. Bobby
    May 28, 2012 at 10:52 pm

    Thanks — this saved me a lot of time!

  28. Andre
    June 6, 2012 at 11:34 am

    Unbelievable, Thanks a lot! I have one more question, Will everything works well when I deploy to a production environment? Is there any extra configuration?

    • Serge
      June 6, 2012 at 12:13 pm

      It is just within VS environement. I will work normally under deployement

  29. Marcus
    July 24, 2012 at 8:24 pm

    Thank you Serge !

  30. Mark
    August 4, 2012 at 7:16 am

    Likewise – I’ve just killed a couple of hours trying to figure this out. Great to finally see the answer!

  31. August 8, 2012 at 5:47 pm

    I really feel like kicking Microsoft in the balls right now. Does anybody now what those options actually do?

  32. Rafał
    September 4, 2012 at 2:09 pm

    Maybye you now how to fix it on wp7?

    • Serge
      September 4, 2012 at 2:20 pm

      unfortunatly never do wcf on wp7 so far

  33. Mona
    October 23, 2012 at 9:55 am

    Hello , Thanks for this post.It solved my problem in seconds. I have spend so many hours solving this issue.

    Great Help….

  34. Dominic
    December 10, 2012 at 10:28 am

    OMG!! Thanks a lot!!

  35. Javier
    April 1, 2013 at 10:21 pm

    Thanks this kind of errors take a lot of time, and when you are looking for solutions you feel the only person with the problem and a like a fool, you see a lot of people explaining it in the same way as you do, and you percibe some like !! Where is the diference !! and so one day after other… Really thanks

  36. Sundar
    April 12, 2013 at 4:54 am

    OMG….. Thanks a lot.!!!!!

  37. René
    April 30, 2013 at 1:52 pm

    Thanks a lot. You saved my day/week/…

  38. shashank
    June 5, 2013 at 11:22 am

    thanks it worked for me too and saved lot of time.

  39. Vamsi Krishna Anupoju
    September 26, 2013 at 9:15 am

    Really you are great!!!!!!!! Thanks a lot….

  40. Hardik
    November 7, 2014 at 7:33 am

    Thanks it worked.

  41. Zavi
    March 8, 2015 at 7:52 pm

    What I find surprising is that Visual Studio designers provide the “A first chance exception of type ‘System.ServiceModel.FaultException`1’ occurred in WCF Service.dll” message, without a corresponding suggestion to disable this under Tools/Options, or even an indication to suggest that this is not programmer error but a feature of the debugger which by default is turned on, as budding developers new to the environment are so obviously bamboozled.

    Clearly, the Visual Studio designers have overlooked the potential to reflect on the service to discover if FaultContract attributes exist and if so suppress the warnings.

    I’m afraid it’s a sad reflection on the industry when, the desire to keep ahead of the competition and set the technological pace of change, that basic instruction (e.g. MSDN Help) is provided only for those, and only understandable by those with the benefit of years of experience.

    Thanks to you Serge for sharing the solution to your problem after spending so many fruitless and wasted hours on something that should be clearly documented – the world is a better place for folks like you.

  42. J
    • Serge
      May 28, 2015 at 7:24 am

      Thanks for the info. The image is back on the post, no idea how it has been removed 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: