c# - Now that WebServiceHost2Factory is dead, how do I return error text from a WCF rest service? -
i've seen several references webservicehost2factory class use effectively handle errors in wcf rest services. apparently class, had throw webprotocolexception , body of response contain pertinent information.
that class seems have fallen out of favor now. there replacement somewhere in .net 4 stack?
i'm trying figure out how return error text in body of response post operation, if went wrong. key question below next *'s
example:
[description("performs full enroll , activation of member 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<loyaltyexception> fex) { log.errorformat("[loyalty full enroll] caught faultexception attempting 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 full enroll. exception: {0}", e); handleexception("fullenroll", e); } return ret; } /// <summary> /// deals w/ response when exceptions thrown /// </summary> private static exception handleexception(string function, exception e, string statusmessage = null) { // set return context, handle error if (weboperationcontext.current != null) { var response = weboperationcontext.current.outgoingresponse; // set error code based on exception var errornum = 500; if (e httpexception) errornum = ((httpexception)e).errorcode; response.statuscode = (httpstatuscode) enum.parse(typeof (httpstatuscode), errornum.tostring()); response.statusdescription = statusmessage; // **************************************************** // how can return body of web method? // **************************************************** weboperationcontext.current.createtextresponse(statusmessage); } return (e httpexception) ? e : new httpexception(500, string.format("{0} caught exception", function)); }
this answer seems suggest using following,
httpcontext.current.response.write(statusmessage);
edit - tobyb mentioned in comments, aspnetcompatibility
required.
here's how turn on:
<system.servicemodel> <servicehostingenvironment aspnetcompatibilityenabled="true"> <!-- ... --> </system.servicemodel>
Comments
Post a Comment