I saw several references to WebServiceHost2Factory as a class that is effectively used to handle errors in WCF Rest services. Apparently, with this class I just had to throw a WebProtocolException, and the response text will contain the relevant information.
This class seems to have not liked. Is there a replacement in the .NET 4 stack?
I am trying to figure out how to return the error text to the body of the response to the POST operation if something went wrong. The key question below is next to everyone * * s
Example:
[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
{
}
catch (FaultException<LoyaltyException> 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;
}
private static Exception HandleException(string function, Exception e, string statusMessage = null)
{
if (WebOperationContext.Current != null)
{
var response = WebOperationContext.Current.OutgoingResponse;
var errorNum = 500;
if (e is HttpException)
errorNum = ((HttpException)e).ErrorCode;
response.StatusCode = (HttpStatusCode) Enum.Parse(typeof (HttpStatusCode), errorNum.ToString());
response.StatusDescription = statusMessage;
WebOperationContext.Current.CreateTextResponse(statusMessage);
}
return (e is HttpException) ? e : new HttpException(500, string.Format("{0} caught an exception", function));
}
tobyb source
share