Note that there are some explanatory texts on larger screens.

plurals
  1. PONow that WebServiceHost2Factory is dead, how do I return error text from a WCF rest service?
    primarykey
    data
    text
    <p>I've seen several references to WebServiceHost2Factory as the <a href="http://www.robbagby.com/rest/effective-error-handling-with-wcf-rest/" rel="nofollow">class to use to effectively</a> handle errors in WCF Rest services. Apparently with that class, I just had to throw a WebProtocolException and the body of the response would contain pertinent information. </p> <p>That class seems to have fallen out of favor now. Is there a replacement somewhere in the .NET 4 stack?</p> <p>I'm trying to figure out how to return error text in the body of a response to a POST operation, if something went wrong. The key question is below next to all the *'s</p> <p>Example:</p> <pre><code>[Description("Performs a full enroll and activation of the member into the Loyalty program")] [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/fullenroll/{clientDeviceId}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] public MemberInfo FullEnroll(string clientDeviceId, FullEnrollmentRequest request) { Log.DebugFormat("FullEnroll. ClientDeviceId: {0}, Request:{1}", clientDeviceId, request); MemberInfo ret = null; try { //Do stuff } catch (FaultException&lt;LoyaltyException&gt; fex) { Log.ErrorFormat("[loyalty full enroll] Caught faultexception attempting to full enroll. Message: {0}, Reason: {1}, Data: {2}", fex.Message, fex.Reason, fex.Detail.ExceptionMessage); HandleException("FullEnroll", fex, fex.Detail.ExceptionMessage); } catch (Exception e) { Log.ErrorFormat( "[loyalty full enroll] Caught exception attempting to full enroll. Exception: {0}", e); HandleException("FullEnroll", e); } return ret; } /// &lt;summary&gt; /// Deals w/ the response when Exceptions are thrown /// &lt;/summary&gt; private static Exception HandleException(string function, Exception e, string statusMessage = null) { // Set the return context, handle the error if (WebOperationContext.Current != null) { var response = WebOperationContext.Current.OutgoingResponse; // Set the error code based on the Exception var errorNum = 500; if (e is HttpException) errorNum = ((HttpException)e).ErrorCode; response.StatusCode = (HttpStatusCode) Enum.Parse(typeof (HttpStatusCode), errorNum.ToString()); response.StatusDescription = statusMessage; // **************************************************** // How can I return this as the body of the Web Method? // **************************************************** WebOperationContext.Current.CreateTextResponse(statusMessage); } return (e is HttpException) ? e : new HttpException(500, string.Format("{0} caught an exception", function)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload