コードに例外がある場合、それがRESTであるかSOAPであるかに応じて、FaultException/WebFaultExceptionとして返したいと思います。次の問題は、私のプロジェクトのRESTコンポーネントにあります。例外がトリガーされ、デバッグウィンドウに表示されます(図を参照)。
WebFaultExceptionがトリガーされていることがはっきりとわかりますが、JSONでの応答はhttp badrequestでも例外メッセージでもありませんが、常に次のようになります。
{"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);
}
}