Click here to Skip to main content
15,885,309 members
Articles / WCF

Error Handling in WCF RESTful Service

Rate me:
Please Sign up or sign in to vote.
4.44/5 (8 votes)
21 Jan 2014CPOL2 min read 34K   6   3
Error Handling in WCF RESTful Service

WCF possesses the capability to handle errors for RESTful services and return appropriate HTTP status code as well as error details using standard formats like JSON or XML. So, WebFaultException is the class used to return:

  • HTTP status code only, or
  • HTTP status code and user-defined type

We can define a custom type for error details and pass it to WebFaultException class constructor. Further, this custom type is serialized using defined format (i.e., JSON/XML) and forwarded to the client.

This WCF tutorial is part of a series on Creating WCF RESTful services. In previous articles, we created RESTful services performing all CRUD operations and later in Part 2, consuming it using jQuery. Here in this part, our focus is on error handling mechanism supported by WCF for HTTP services. We will be referring to the previous article example here, so I'll recommend to go through it first.

Now, it's quite simple to return HTTP status code only. For example in the previous article, we created a WCF RESTful service having a service method GetBookById. It takes a bookId as method parameter and return Book object against provided bookId in JSON format. So, in order to return proper HTTP status code, we can update the implementation as follows:

C#
public Book GetBookById(string id)
 {
            Book book = repository.GetBookById(int.Parse(id));
            if (book == null)
            {
                throw new WebFaultException(HttpStatusCode.NotFound);
            }
            return book;
 }

But if we wanted to add some custom details with error code, we need to provide a user-defined type that holds the information. I have created one simple custom error type but you can modify it according to your need.

C#
[DataContract]
public class MyCustomErrorDetail
{
       public MyCustomErrorDetail(string errorInfo, string errorDetails)
       {
           ErrorInfo = errorInfo;
           ErrorDetails = errorDetails;
       }

       [DataMember]
       public string ErrorInfo  { get; private set; }

       [DataMember]
       public string ErrorDetails { get; private set; }
}

And implementation for the same service method GetBookById will be updated accordingly as follows:

C#
public Book GetBookById(string id)
 {
            Book book = repository.GetBookById(int.Parse(id));
            if (book == null)
            {
                MyCustomErrorDetail customError = new MyCustomErrorDetail("Book not found",
                "No book exists against provided bookId.");
                throw new WebFaultException<MyCustomErrorDetail>(customError, HttpStatusCode.NotFound);
            }
            return book;
 }

Hopefully, this article will be helpful in custom error handling for WCF RESTful service.

Readers of this article might also be interested in:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
PraiseThank you Pin
Massimo Laboranti31-Aug-20 4:40
Massimo Laboranti31-Aug-20 4:40 
QuestionMy vote of 5: Simple but effective Pin
CasaSpider3-Mar-16 2:00
CasaSpider3-Mar-16 2:00 
GeneralMy vote of 3 Pin
maryjainpa20-May-14 2:30
professionalmaryjainpa20-May-14 2:30 
not must content on concept

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.