Error Handling In Rest Wcf
Contents |
DotNetBloggerJanuary 21, 20100 0 0 0 This is part five of a twelve part series that introduces the features of WCF WebHttp wcf 4.0 rest error handling Services in .NET 4. In this post we will cover: Handling Errors with
Error Handling In Wcf Rest Service
the WebFaultException class Over the course of this blog post series, we are building a web service called TeamTask.
Exception Handling In Wcf Rest Service
TeamTask allows a team to track tasks assigned to members of the team. Because the code in a given blog post builds upon the code from the previous posts, the posts
Wcf Error Handling Best Practices
are intended to be read in-order. Downloading the TeamTask Code At the end of this blog post, you’ll find a link that will allow you to download the code for the current TeamTask Service as a compressed file. After extracting, you’ll find that it contains “Before” and “After” versions of the TeamTask solution. If you would like to follow along with the wcf exception handling fault contract steps outlined in this post,download the code and open the "Before" solution in Visual Studio 2010. If you aren’t sure about a step, refer to the “After” version of the TeamTask solution. Note: If you try running the sample code and see a Visual Studio Project Sample Loading Error that begins with “Assembly could not be loaded and will be ignored…”, see here for troubleshooting. Getting Visual Studio 2010 To follow along with this blog post series, you will need to have Microsoft Visual Studio 2010 and the full .NET 4 Framework installed on your machine. (The client profile of the .NET 4 Framework is not sufficient.) At the time of this posting, the Microsoft Visual Studio 2010 Ultimate Beta 2 is available for free download and there are numerous resources available regarding how to download and install, including this Channel 9 video. Step 1: Handling Non-Existent Users In the very first post of this series, we implemented the GetUser() service operation. The GetUser() service operation returns the details of a user given a username provided in the request URI. A precondition of the GetUser()
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 wcf exception handling client side is serialized using defined format (i.e. JSON/XML) and forwarded to the client. :::::A Practical Guide to WCF RESTful Service::::: This wpf error handling WCF tutorial is part of series on Creating WCF RESTful services. In previous articles, we created RESTful services peforming all CRUD operations and later in Part-2, consuming it using jQuery. linq error handling Here in this part, our focus is on error handling mechanism supported by WCF for HTTP services. We will be referring the previous article example here, so I'll recommend to go through it first. Now, its quite simple to return HTTP status code only. For example in previous article, we https://blogs.msdn.microsoft.com/endpoint/2010/01/21/error-handling-in-wcf-webhttp-services-with-webfaultexception/ 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: public Book GetBookById(string id) { Book book = repository.GetBookById(int.Parse(id)); if (book == null) { throw new WebFaultException(HttpStatusCode.NotFound); } return book; http://www.topwcftutorials.net/2014/01/error-handling-wcf-restful-service.html } 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. [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 methodGetBookById will be updated accordingly as follows: 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
services. In this post, I will illustrate how to expose, as well as handle errors effectively when working http://www.robbagby.com/rest/effective-error-handling-with-wcf-rest/ with RESTful WFC services. Watch the Screencast Watch the screencast on Channel9. The Sample Service As you can see from the ServiceHost directive, we are using the http://kenneththorman.blogspot.com/2011/02/wcf-rest-exception-handling.html configurationless model. By setting the factory to WebServiceHostFactory, we do not need to define all of the configuration settings in the web.config under System.serviceModel. The WebServiceHostFactory will dynamically error handling set up an instance of the WebServiceHost in response to requests. The WebServiceHost will set up a default endpoint, if appropriate, with the binding set to webHttpBinding (read REST-Friendly binding). Further, this host will add the WebHttpBehavior to the endpoints. The WebServiceHostFactory is our RESTful factory and first shipped with WCF 3.5 (with Visual Studio 2008 error handling in sp1). Examining the service implementation, it is clear that this is a relatively simple service operation that we are exposing. The operation returns a page of wines from our wine catalog (by page, I am referring to returning a subset of wines (1 to x out of y) instead of the complete set). We decorated the operation with a WebGet, exposing the service over an HTTP GET. We further set the ResponseFormat to JSON and set up a friendly UriTemplate. If the above concepts are foreign to you, or you want a refresher on the basics of REST in WCF, check out all of my blog posts on the subject. The last thing to notice is that if someone passes an invalid pageSize, we are throwing an ApplicationException. This is what you might expect to see in a typical .NET implementation. The next step is to cover the basics of errors in RESTful services. REST and Errors As you probably know, the underlying theme of REST i
how WCF REST handles service faults compared to other WCF implementations. Ground rules: In WCF it is not recommended to throw .NET exceptions since they most likely are not known and recognized in non .NET clients. so instead we are using FaultException