2

私は FtpWebRequest と Transient Fault Handling アプリケーション ブロックを使用しています。私の障害ハンドラーには、応答が一時的であると見なされるかどうかをチェックして、再試行するかどうかを判断するエラー検出戦略があります。

public bool IsTransient(Exception ex)
    {

        var isTransient = false;
        //will be false if the exception is not a web exception.
        var webEx = ex as WebException;

        //happens when receiving a protocol error.
        //This protocol error wraps the inner exception, e.g. a 401 access denied.
        if (webEx != null && webEx.Status == WebExceptionStatus.ProtocolError)
        {
            var response = webEx.Response as FtpWebResponse;
            if (response != null && (int)response.StatusCode < 400)
            {
                isTransient = true;
            }
        }
        // if it is a web exception but not a protocol error,
        // check the status code.
        else if (webEx != null)
        {
            //(check for transient error statuses here...)
            isTransient = true;
        }

        return isTransient;
    }

適切なエラーが一時的なものとしてフラグ付けされていることを確認するためにいくつかのテストを作成しようとしていますが、FtpWebResponse で内部例外を持つ Web 例外を作成またはモックするのに問題があります (したがって、次の応答は ' t は常にヌル)

var response = webEx.Response as FtpWebResponse;

どうすればこれができるか知っている人はいますか?私はそれを正しい方法で行っていますか?

4

2 に答える 2