内部异常的属性设置? [英] Properties of Inner Exception are Disposed?

查看:179
本文介绍了内部异常的属性设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个WCF Web服务的单元测试。我故意发送一个无效的请求给服务器,抛出一个 WebExceptionFault<串GT; 。在单元测试,这被逮住的异常是 EndpointNotFoundException 与在引发WebException >的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.

我来翻过一个问题,但是,由于的响应属性引发WebException (其为 System.Net.SyncMemoryStream )被设置,并且不能被读取

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.

我的代码:

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的属性被设置?有没有解决这个办法吗?

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

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

这篇关于内部异常的属性设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆