5

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?

ARZ
  • 2,461
  • 3
  • 34
  • 56
Clivest
  • 499
  • 1
  • 4
  • 14
  • 8
    That's to be expected. System.Net isn't going to assume that whomever catches the exception is going to go through the trouble of digging out the response stream and dispose it. So it cleans up before throwing. You can't change this behavior. – Hans Passant Jun 25 '11 at 18:42
  • Surely you shouldn't be able to access the response stream if the underlying connection is closed right? At that point, you've almost certainly closed it by the time you start processing the `EndPointNotFoundException` exception. I think you should just start processing the exception as it is caught reading in the response stream immediately and wrapping that up. – Jeff Mercado Jun 25 '11 at 18:43
  • @Jeff: When is the underlying connection closed in the above example? The earliest point I can see to read the response stream is after getting the `HttpWebResponse`. Is that where you mean? – Clivest Jun 25 '11 at 19:14
  • I figured that you have a `using` block in your constructor somewhere for your connection. Once out of scope, the connection would be closed. If there's some details that I misunderstood about WCF or your situation, I apologize. – Jeff Mercado Jun 25 '11 at 19:24
  • @Jeff: Yes, I've got a using statement that creates the channel, but it is still in scope there. @Hans: Thanks. I guess that answers my question then. – Clivest Jun 25 '11 at 19:51

1 Answers1

0

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#?

Community
  • 1
  • 1
Pinakin Shah
  • 877
  • 2
  • 12
  • 25