【问题标题】:Now that WebServiceHost2Factory is dead, how do I return error text from a WCF rest service?现在 WebServiceHost2Factory 已死,如何从 WCF 休息服务返回错误文本?
【发布时间】:2013-03-12 19:16:55
【问题描述】:

我已经看到几个对 WebServiceHost2Factory 的引用,因为 class to use to effectively 处理 WCF Rest 服务中的错误。显然对于那个类,我只需要抛出一个 WebProtocolException 并且响应的主体将包含相关信息。

这个班级现在似乎已经失宠了。 .NET 4 堆栈中的某个地方有替代品吗?

我试图弄清楚如果出现问题,如何在对 POST 操作的响应正文中返回错误文本。关键问题在下面所有 * 的旁边

例子:

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

【问题讨论】:

    标签: c# .net wcf rest


    【解决方案1】:

    This answer 似乎建议使用以下内容,

    HttpContext.Current.Response.Write(statusMessage);
    

    编辑 - 作为 cmets 中的 tobyb mentioned,AspNetCompatibility 是必需的。

    开启方法如下:

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

    【讨论】:

    • 行得通!谢谢。唯一需要注意的是,该服务需要 AspNetCompatibility,这对我来说不是问题,但对其他人来说可能是问题。再次感谢!
    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多