C# Rest Web Service Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have web service error handling best practices Meta Discuss the workings and policies of this site About Us
Web Service Error Handling Best Practices Java
Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with
Web Service Exception Handling C#
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers,
C# Error Handling In Constructor
just like you, helping each other. Join them; it only takes a minute: Sign up Handling WCF Rest Service exceptions only in one place up vote 2 down vote favorite 1 I'm developing an WCF Rest service that I'm going to host on an IIS. Now I'm implementing Service Contract, and I see that I'm repeating c# error handling get line number the same code on all of my methods when I'm trying to handle exceptions. This is one of my Service Contract method: public void DeleteMessage(string message_id) { int messageId; OutgoingWebResponseContext ctx = WebOperationContext.Current.OutgoingResponse; if ((message_id == null) || (!Int32.TryParse(message_id, out messageId)) || (messageId < 1)) { ctx.StatusCode = System.Net.HttpStatusCode.BadRequest; ctx.StatusDescription = "message_id parameter is not valid"; throw new ArgumentException("DeleteMessage: message_id is not valid", "message_id"); } try { using (var context = new AdnLineContext()) { Message message = new Message() { MessageId = messageId }; context.Entry(message).State = EntityState.Deleted; context.SaveChanges(); ctx.StatusCode = System.Net.HttpStatusCode.OK; } } catch (Exception ex) { ctx.StatusCode = System.Net.HttpStatusCode.InternalServerError; ctx.StatusDescription = ex.Message; ctx.SuppressEntityBody = true; } } On all of my methods I could throw an ArgumentException or an Exception, and manage them with return a HTTP STATUS CODE. Is there any way to catch that exceptions globally? c# wcf rest share|improve this question edited Aug 7 '13 at 16:14 asked Aug 7 '13 at 15:41 VansFannel 14k57229406 add a comment| 2 Answers 2 act
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 c# error handling framework only, or HTTP status code and user-defined type. We can define a custom type c# error handling techniques for error details and pass it to WebFaultException class constructor. Further, this custom type is serialized using defined format (i.e. JSON/XML) c# error handling class and forwarded to the client. :::::A Practical Guide to WCF RESTful Service::::: This WCF tutorial is part of series on Creating WCF RESTful services. In previous articles, we created RESTful services peforming all CRUD operations and http://stackoverflow.com/questions/18107743/handling-wcf-rest-service-exceptions-only-in-one-place 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 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 created a WCF RESTful service having a service method GetBookById. It takes a bookId as http://www.topwcftutorials.net/2014/01/error-handling-wcf-restful-service.html 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; } 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
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of http://programmers.stackexchange.com/questions/237237/how-to-handle-exception-in-rest-api this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it error handling works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to handle exception in REST API up vote 2 down vote favorite 1 Given a REST API, which throws an exception when handling a request from a client. Whenever the REST API throws an exception, it's logged and the application (in this c# error handling case a console application) closes to prevent further damage. Should I send a response to the client whose request resulted in an exception, indicating that an error occured or should I let the client hang and deal with it (timeout)? c# .net api rest share|improve this question asked Apr 28 '14 at 9:42 ebb 1135 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote accepted I've never seen any API letting the client hanging in a case of an error. Unless you have a very specific case where this makes senseĀ¹ This simply doesn't make sense, and such behavior would only result in a practically unusable API: This would slow the development of the client applications. Unless I read very, very carefully the documentation (and the documentation is excellent), I'll make mistakes which would result in timeouts, and thus slow development. This would also make the client apps slow if they cause exceptions when used by an end user. So no, don't do that. What you should do is to handle the error gracefully. This means sending a response contain