Error Handling in WCF with WebFaultException(.Net 4.0)

Posted: March 10, 2011 in WCF
Tags: ,

A sad afternoon & was feeling quite helpless few hours back.Suddenly like a piece of choclate the word WebFaultException came in &  I decided to forget everything & write about this 🙂 Well stories apart,lets get into the point. Consider having a situation,where you search for a particular user by hitting a wcf service.If the user is not found(say your database server is down),it will straight away throw an exception to client saying HTTP Error 404 Not found.I swear, the end user will not understand the actual meaning behind this.
Such cases,use the WebFaultException class to set the HTTP status code to 404 (Not Found) and give indication of the error description in the message body. 

throw new WebFaultException<string>(
            string.Format("There is no user available with the provided userName '{0}'.", userName),

Hope this helps you 🙂 This is my basic understanding about WebFaultException & I would appreciate if any of you can share some more information you have about this.

Leave a Reply

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

You are commenting using your 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