Widziałem kilka odwołań do WebServiceHost2Factory jako błędy obsługi w usługach WCF Rest. Najwyraźniej z tą klasą, musiałem rzucić wyjątek WebProtocolException, a treść odpowiedzi zawierałaby istotne informacje.Teraz, gdy WebServiceHost2Factory nie działa, jak zwrócić tekst błędu z usługi odtwarzania WCF?
Ta klasa wydaje się być teraz nieużyteczna. Czy gdzieś w stosie .NET 4 znajduje się zamiennik?
Próbuję dowiedzieć się, jak zwrócić tekst błędu w treści odpowiedzi na operację POST, jeśli coś poszło nie tak. Kluczową kwestią jest poniżej obok wszystkich * 's
Przykład:
[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<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;
}
/// <summary>
/// Deals w/ the response when Exceptions are thrown
/// </summary>
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));
}
To działa! Dzięki. Jedynym zastrzeżeniem jest to, że usługa wymaga AspNetCompatibility, która dla mnie nie jest problemem, ale może być dla innych. Dzięki jeszcze raz! – tobyb
Świetnie, cieszę się, że pomogło! – Jesse