处理内部异常的属性?

Cli*_*est 5 c# exception-handling exception inner-exception

我正在为WCF Web服务编写单元测试.我故意向服务器发送无效请求,抛出一个WebExceptionFault<string>.在单元测试中,被抓住的异常是EndpointNotFoundException与标准WebExceptionInnerException属性.我想验证响应的主体是否匹配我认为应该存在的字符串.

然而,我遇到了一个问题,因为(这是一个)的Response属性是Disposed并且无法读取.WebExceptionSystem.Net.SyncMemoryStream

我的代码:

Clubs actual;
try
{
    //"201" is an invalid argument for the first parameter
    actual = new Clubs(channel.GetClubs("201", "123"));
}
catch (EndpointNotFoundException e)
{
    WebException w = e.InnerException as WebException;
    Assert.IsNotNull(w);

    HttpWebResponse resp = w.Response as HttpWebResponse;
    Assert.IsNotNull(resp);
    Assert.AreEqual(resp.StatusCode, HttpStatusCode.NotFound);
    //Next line throws an ArgumentException saying Stream not readable
    //Investigation shows it has been disposed
    using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
        Assert.AreEqual(sr.ReadToEnd(), "League not allowed for selected club");
}
Run Code Online (Sandbox Code Playgroud)

是否正常处理InnerException的属性?有没有办法解决?

Pin*_*hah 0

似乎是异常序列化问题。您可能必须捕获服务器上的错误并自定义构建错误消息

如何在 C# 中序列化 Exception 对象?