2012-12-04 27 views
1

我有工作WCF REST Web服務,可以設置狀態碼和狀態說明像往常一樣:如何強制WebFaultException返回自定義錯誤?

OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; 
response.StatusCode = statusCode; 
response.StatusDescription = detail.Error; 

但我想用WebFaultException。不幸的是,alvays返回{「詳細信息」:「未找到」}當我運行我的代碼:

[Serializable] 
[DataContract] 
public class DtoError 
{ 
    public DtoError() 
    { 

    } 
    public DtoError(string error) 
    { 
     Error = error; 
    } 
    [DataMember] 
    public string Error { get; private set; } 
} 

var error = new DtoError(entityName + " is not existing"); 
throw new WebFaultException<DtoError>(error, HttpStatusCode.NotFound); 

我能回到我的自定義錯誤JSON對象?

回答

0

經過調查後,我發現我們使用Rest Starter Kit中的WebServiceHost2而不是WebServiceHost。在該主機內部,我們必須使用WebProtocolException。所以現在我的工作代碼看起來像這樣:

throw new WebProtocolException(HttpStatusCode.NotFound, "Not Found", error, null);