代码之家  ›  专栏  ›  技术社区  ›  sixeyes

WebException.状态未序列化

  •  3
  • sixeyes  · 技术社区  · 12 年前

    当我运行下面的测试(NUnit和VS2008SP1)时,我得到以下错误

    预期:ConnectFailure
    但是:未知错误

    有人能解释我做错了什么吗?

    [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));
        }
    }
    
    1 回复  |  直到 12 年前
        1
  •  1
  •   zuraff    10 年前

    你做得很好。在我看来,这是.NET框架中的一个bug。在处理另一个AppDomain中引发的异常时,在.NET4.6中遇到了这一点。 http://referencesource.microsoft.com/#System/net/System/Net/WebException.cs,47e37066345d8b7c 显示此字段的序列化代码已注释掉。

    推荐文章