是否有可能獲得WCF服務來向客戶端返回'錯誤'?我被認爲使用SOAP時這是可能的,但我想返回JSON。WCF服務返回JSON格式的錯誤
理想情況下,HTTP響應代碼將被設置爲指示發生錯誤的內容,然後在JSON響應中提供該問題的詳細信息。
目前,我正在做這樣的事情:
[ServiceContract]
public class MyService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[FaultContract(typeof(TestFault))]
public MyResult MyMethod()
{
throw new FaultException<TestFault>(new TestFault("Message..."), "Reason...");
}
}
凡TestFault
看起來是這樣的:
[DataContract]
public class TestFault
{
public TestFault(string message)
{
this.Message = message;
}
[DataMember]
public string Message { get; set; }
}
有沒有在服務配置尤爲特別的時刻。
這會導致'400錯誤請求'響應,並顯示HTML格式的錯誤。 (當我includeExceptionDetailInFaults
,我可以看到「理性......」和FaultException
的細節,但對TestFault
沒有詳細說明。)
的web服務,返回JSON OK了Exception
(或FaultException
)是不是被當拋出。
任何建議..?
又一次嘗試在解決這一這裏http://iainjmitchell.com/blog/?p=142 – 2011-06-20 14:33:23