2013-02-05 42 views
2

当我的代码中有异常时,我想根据它是REST还是SOAP,将它作为FaultException/WebFaultException返回。以下问题与我的项目的REST组件有关。异常被触发,我在我的调试窗口中看到它(见图片)。WCF Rest WebFaultException总是返回202

enter image description here

我们清楚地看到,WebFaultException被触发,但我的JSON响应不是HTTP错误请求也不是我的异常消息有,但始终执行以下操作:

{"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); 
    } 

}

+0

您是否设法解决了此问题?我被卡住了(是)。 –

回答

0

我看到一些帖子,其中问题是在WcfRestContrib自定义错误处理。在这些实例中,将服务类属性从[ServiceConfiguration(“Rest”,true)]更改为[ServiceConfiguration(“Rest”,false)]最终解决了问题。或者,您可以将其保留为true,并尝试使用WebException而不是WebFaultException,如下所示:

throw new 
    WcfRestContrib.ServiceModel.Web.Exceptions.WebException(HttpStatusCode.BadRequest, "My custom error!");