2013-03-12 2 views
5

Ich habe mehrere Verweise auf WebServiceHost2Factory gesehen, da die class to use to effectively Fehler in WCF Rest Services behandelt. Anscheinend mit dieser Klasse, ich musste nur eine WebProtocolException werfen und der Körper der Antwort würde relevante Informationen enthalten.Nun, da WebServiceHost2Factory tot ist, wie gebe ich Fehlertext von einem WCF-Restdienst zurück?

Diese Klasse scheint jetzt in Ungnade gefallen zu sein. Gibt es irgendwo im .NET 4-Stack einen Ersatz?

Ich versuche herauszufinden, wie Fehlertext im Körper einer Antwort auf eine POST-Operation zurückgegeben wird, wenn etwas schief gelaufen ist. Die entscheidende Frage ist unten neben den ganzen * 's

Beispiel:

[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)); 
} 

Antwort

4

This answer scheint folgendes vorschlagen verwenden,

HttpContext.Current.Response.Write(statusMessage); 

bearbeiten - Wie tobyb mentioned in die Kommentare, AspNetCompatibility ist erforderlich.

Hier ist, wie es einzuschalten:

<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"> 
    <!-- ... --> 
</system.serviceModel> 
+0

, das funktioniert! Vielen Dank. Der einzige Nachteil ist, dass der Dienst AspNetCompatibility erfordert, was für mich kein Problem ist, aber möglicherweise für andere. Danke noch einmal! – tobyb

+0

Großartig, froh, dass es geholfen hat! – Jesse

Verwandte Themen