问题描述
我正在为WCF Web服务编写单元测试。我故意向服务器发送一个无效的请求,该请求会引发一个 WebExceptionFault< string>
。在单元测试中,被捕获的异常是 EndpointNotFoundException
,标准 WebException
在 InnerException
属性。我想验证响应的身体匹配我认为应该在那里的字符串。 然而,我正在遇到一个问题,因为 WebException的
(这是一个响应
属性 System.Net.SyncMemoryStream
)被处理,无法读取。
我的代码:
俱乐部实际;
try
{
//201是第一个参数的无效参数
actual = new Clubs(channel.GetClubs(201,123));
}
catch(EndpointNotFoundException e)
{
WebException w = e.InnerException as WebException;
Assert.IsNotNull(w);
HttpWebResponse resp = w.Response作为HttpWebResponse;
Assert.IsNotNull(resp);
Assert.AreEqual(resp.StatusCode,HttpStatusCode.NotFound);
//下一行抛出一个ArgumentException说流不可读
//调查显示已经被处理
(StreamReader sr = new StreamReader(resp.GetResponseStream()))
Assert.AreEqual(sr.ReadToEnd(),联盟不允许选定俱乐部);
}
InnerException的属性是否正常处理?有没有办法呢?
似乎是一个异常序列化问题。您可能必须在服务器和自定义上捕获错误,构建错误消息
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?
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#?
这篇关于内部异常的属性被处置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!