Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 既然WebServiceHost2Factory已经死了,我如何从WCF rest服务返回错误文本?_C#_.net_Wcf_Rest - Fatal编程技术网

C# 既然WebServiceHost2Factory已经死了,我如何从WCF rest服务返回错误文本?

C# 既然WebServiceHost2Factory已经死了,我如何从WCF rest服务返回错误文本?,c#,.net,wcf,rest,C#,.net,Wcf,Rest,我已经看到一些对WebServiceHost2Factory的引用,作为WCF Rest服务中的句柄错误。显然,对于那个类,我只需抛出一个WebProtocolException,响应的主体将包含相关信息 那门课现在似乎不受欢迎了。在.NET4堆栈的某个地方是否有替换 我试图弄清楚,如果出现问题,如何在对POST操作的响应主体中返回错误文本。下面是所有*旁边的关键问题 例如: [Description("Performs a full enroll and activation of the m

我已经看到一些对WebServiceHost2Factory的引用,作为WCF Rest服务中的句柄错误。显然,对于那个类,我只需抛出一个WebProtocolException,响应的主体将包含相关信息

那门课现在似乎不受欢迎了。在.NET4堆栈的某个地方是否有替换

我试图弄清楚,如果出现问题,如何在对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));
}
[说明(“将会员完全注册并激活至忠诚计划”)]
[经营合同]
[WebInvoke(Method=“POST”,UriTemplate=“/fullenroll/{clientDeviceId}”,
BodyStyle=WebMessageBodyStyle.Bare,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json)]
public MemberInfo FullEnroll(字符串clientDeviceId,FullEnrollmentRequest)
{
DebugFormat(“FullEnroll.ClientDeviceId:{0},请求:{1}”,ClientDeviceId,请求);
MemberInfo ret=null;
尝试
{
//做事
}
捕获(故障异常fex)
{
Log.ErrorFormat(“[loyalty full enroll]捕获到尝试完全注册的错误异常。消息:{0},原因:{1},数据:{2}”,fex.Message,fex.Reason,fex.Detail.ExceptionMessage);
HandleException(“FullEnroll”,fex,fex.Detail.ExceptionMessage);
}
捕获(例外e)
{
Log.ErrorFormat(
“[忠诚度完全注册]捕获到尝试完全注册的异常。异常:{0}”,e);
HandleException(“完全注册”,e);
}
返回ret;
}
/// 
///在抛出异常时处理与响应
/// 
私有静态异常HandleException(字符串函数,异常e,字符串statusMessage=null)
{
//设置返回上下文,处理错误
if(WebOperationContext.Current!=null)
{
var response=WebOperationContext.Current.OutgoingResponse;
//根据异常设置错误代码
var-errorNum=500;
if(e是HttpException)
errorNum=((HttpException)e).ErrorCode;
response.StatusCode=(HttpStatusCode)Enum.Parse(typeof(HttpStatusCode),errorNum.ToString());
response.StatusDescription=状态消息;
// ****************************************************
//如何将其作为Web方法的主体返回?
// ****************************************************
WebOperationContext.Current.CreateTextResponse(statusMessage);
}
返回(e是HttpException)?e:新的HttpException(500,string.Format(“{0}捕获了一个异常”,function));
}
似乎建议使用以下方法:

HttpContext.Current.Response.Write(statusMessage);

编辑-与注释中一样,需要
AspNetCompatibility

以下是如何打开它:


这很有效!谢谢唯一需要注意的是,该服务需要AspNetCompatibility,这对我来说不是问题,但对其他人来说可能是问题。再次感谢!