5

我正在为 WCF Web 服务编写单元测试。我故意向抛出WebExceptionFault<string>. 在单元测试中,被捕获的异常是属性中EndpointNotFoundException的标准。我想验证响应的正文是否与我认为应该存在的字符串匹配。WebExceptionInnerException

但是,我遇到了一个问题,因为(它是 a )的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");
}

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

4

1 回答 1

0

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

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

于 2011-07-22T12:33:41.167 回答