C# WCF Rest WebFaultException始终返回202
当我的代码中有一个异常时,我希望根据它是REST还是SOAP将其作为FaultException/WebFaultException返回。以下问题与我的项目的REST组件有关。当异常被触发时,我会在调试窗口中看到它(见图) 我们清楚地看到WebFaultException正在被触发,但我在JSON中的响应不是http badrequest,那里也不是我的异常消息,而是始终如下所示:C# WCF Rest WebFaultException始终返回202,c#,.net,wcf,wcf-rest,C#,.net,Wcf,Wcf Rest,当我的代码中有一个异常时,我希望根据它是REST还是SOAP将其作为FaultException/WebFaultException返回。以下问题与我的项目的REST组件有关。当异常被触发时,我会在调试窗口中看到它(见图) 我们清楚地看到WebFaultException正在被触发,但我在JSON中的响应不是http badrequest,那里也不是我的异常消息,而是始终如下所示: {"readyState":4,"responseText":"","status":202,"statusTe
{"readyState":4,"responseText":"","status":202,"statusText":"Accepted"}
这是我的界面:
[WebInvoke(Method = "POST", UriTemplate = "/Package/", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
[FaultContract(typeof(FaultException))]
[OperationContract]
string CopyZip(Stream zippedPackage);
以下是它的实施:
public string CopyZip(Stream zippedPackage)
{
try
{
ZipCreator pck = new ZipCreator();
pck.CopyUploadedZip(zippedPackage);
return "Zipped";
}
catch (Exception ex)
{
//throw new FaultException(ex.Message);
throw new WebFaultException<string>(ex.Message, HttpStatusCode.BadRequest);
}
公共字符串CopyZip(Stream-zippedPackage)
{
尝试
{
ZipCreator pck=新ZipCreator();
pck.CopyUploadedZip(zippedPackage);
返回“拉链”;
}
捕获(例外情况除外)
{
//抛出新的FaultException(例如消息);
抛出新的WebFaultException(例如Message、HttpStatusCode.BadRequest);
}
}我在一些帖子中看到问题出现在WcfRestContrib自定义错误处理上。在这些情况下,将服务类属性从[ServiceConfiguration(“Rest”,true)]更改为[ServiceConfiguration(“Rest,false)]最终解决了问题。或者,您可以将其保留为true,并尝试使用WebException而不是WebFaultException,如下所示
throw new
WcfRestContrib.ServiceModel.Web.Exceptions.WebException(HttpStatusCode.BadRequest, "My custom error!");
你解决了这个问题吗?我像过去一样被困住了。