You're doing everything fine. To me looks like it is a bug in .NET framework. Stumbled upon this in .NET 4.6 when dealing with exceptions that are thrown in another AppDomain. http://referencesource.microsoft.com/#System/net/System/Net/WebException.cs,47e37066345d8b7c shows that the code for serialization of this field is commented out.
WebException.Status not serialized
-
11-06-2023 - |
Question
When I run the test below (NUnit and VS2008 SP1) I get the following error
Expected: ConnectFailure
But was: UnknownError
Can anyone explain what I'm doing wrong?
[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));
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow