2

コードに例外がある場合、それが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);
   }

}

4

1 に答える 1

0

問題が WcfRestContrib カスタム エラー処理に関する投稿を見たことがあります。そのような場合、サービス クラス属性を [ServiceConfiguration("Rest", true)] から [ServiceConfiguration("Rest", false)] に変更すると、問題が解決しました。または、true のままにして、次のように WebFaultException ではなく WebException を使用してみてください。

throw new
    WcfRestContrib.ServiceModel.Web.Exceptions.WebException(HttpStatusCode.BadRequest, "My custom error!");
于 2015-06-05T18:00:57.043 に答える