0

wcf サービスを呼び出して、IClientMessageInspector インターフェイスを実装しました。

AfterReceiveReply でエラーを処理したい。フォルト例外は、カスタム メッセージとエラー コードを含む 200 ステータス コードを常に返す必要があります。サービス呼び出しで例外をスローするべきではありません。

public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                // wrap message with http status code 200 and return content as exception model like status code, exception message;
            }
        }

例外を処理したいのですが、顧客コードで処理する例外なしで常に応答を返します。

ありがとう

4

1 に答える 1

0

HttpResponseMessageProperty を試すことができます。これには、返されたメッセージのステータス コードが含まれています。

 if (reply.IsFault)
        {
            if (reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))
            {
                HttpResponseMessageProperty pro = reply.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                pro.StatusCode = System.Net.HttpStatusCode.OK;

            }
            else
            {
                reply.Properties.Add(HttpResponseMessageProperty.Name, new HttpResponseMessageProperty { StatusCode = System.Net.HttpStatusCode.OK });
            }
        }

ただし、サーバーからメッセージが返された場合、サーバーが例外をスローした場合、AfterReceiveReply メソッドでメッセージを受信できない場合があります。

これが機能しない場合は、サーバー側で IDispatchMessageInspector を使用してこれを実行し、応答を送信する前にステータス コードを変更することをお勧めします。(サーバー側を制御できる場合) http://mark.mymonster.nl/2011/02/10/make-use-of-wcf-faultcontracts-in-silverlight-clients/

于 2019-01-28T07:35:04.067 に答える