当我运行下面的测试(NUnit 和 VS2008 SP1)时,我得到以下错误
预期:连接失败
但是是:UnknownError
谁能解释我做错了什么?
[Test]
public void SerializationWebExceptionTest()
{
using (var stream = new MemoryStream())
{
const WebExceptionStatus Expected = WebExceptionStatus.ConnectFailure;
var formatter = new BinaryFormatter();
var initialException = new WebException("Test", null, Expected, null);
formatter.Serialize(stream, initialException);
stream.Seek(0, SeekOrigin.Begin);
var result = (WebException)formatter.Deserialize(stream);
var actual = result.Status;
Assert.That(actual, Is.EqualTo(Expected));
}
}
最佳答案
你做的一切都很好。在我看来,这是 .NET 框架中的一个错误。在处理在另一个 AppDomain 中抛出的异常时,在 .NET 4.6 中偶然发现了这一点。 http://referencesource.microsoft.com/#System/net/System/Net/WebException.cs,47e37066345d8b7c显示该字段序列化的代码被注释掉了。
关于c# - WebException.Status 未序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22255836/