Properties of Inner Exception are Disposed?

筅森魡賤 提交于 2020-01-03 12:29:29

问题


I am writing a unit test for a WCF web service. I am deliberately sending an invalid request to the server which throws a WebExceptionFault<string>. In the unit test, the Exception that gets caught is an EndpointNotFoundException with the standard WebException in the InnerException property. I want to verify the body of the response matches the string that I think should be there.

I am coming accross a problem, however, as the Response property of the WebException (which is a System.Net.SyncMemoryStream) is Disposed and can not be read.

My code:

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");
}

Is it normal that the properties of the InnerException are disposed? Is there any way around this?


回答1:


Seems a Exception serialization issue. You might have to trap the error on server and custom build a error message

How to serialize an Exception object in C#?



来源:https://stackoverflow.com/questions/6479771/properties-of-inner-exception-are-disposed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!