2013-02-05 71 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!");