Invalid SecurityProtocolType.Ssl3 enumeration value during XML serialization

I have the following simple XML serializable type:

[XmlType] public class TestType { public System.Net.SecurityProtocolType ProtocolType { get; set; } } var instanceToSerialize = new TestType { ProtocolType = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 }; 

I can serialize an instance of this object on a computer and several computers. But on one of the computers that I tested, I get the following exception:

 System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '4080' is not a valid value for System.Net.SecurityProtocolType. at System.Xml.Serialization.XmlCustomFormatter.FromEnum(Int64 val, String[] vals, Int64[] ids, String typeName) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestType.Write1_SecurityProtocolType(SecurityProtocolType v) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestType.Write3_TestType(String n, String ns, TestType o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestType.Write4_TestType(Object o) 

I compared the generated sgen.exe code for operation and crashes. In the working case, I have this code:

 string Write1_SecurityProtocolType(global::System.Net.SecurityProtocolType v) { string s = null; switch (v) { case global:: System.Net.SecurityProtocolType.@Ssl3 : s = @"Ssl3"; break; case global:: System.Net.SecurityProtocolType.@Tls : s = @"Tls"; break; case global:: System.Net.SecurityProtocolType.@Tls11 : s = @"Tls11"; break; case global:: System.Net.SecurityProtocolType.@Tls12 : s = @"Tls12"; break; default: s = FromEnum(((System.Int64)v), new string[] {@"Ssl3", @"Tls", @"Tls11", @"Tls12"}, new System.Int64[] {(long)global:: System.Net.SecurityProtocolType.@Ssl3 , (long)global:: System.Net.SecurityProtocolType.@Tls , (long)global:: System.Net.SecurityProtocolType.@Tls11 , (long)global:: System.Net.SecurityProtocolType.@Tls12 }, @"System.Net.SecurityProtocolType"); break; } return s; } 

While on failure codes, this is the generated code:

 string Write1_SecurityProtocolType(global::System.Net.SecurityProtocolType v) { string s = null; switch (v) { case global:: System.Net.SecurityProtocolType.@Tls : s = @"Tls"; break; case global:: System.Net.SecurityProtocolType.@Tls11 : s = @"Tls11"; break; case global:: System.Net.SecurityProtocolType.@Tls12 : s = @"Tls12"; break; default: s = FromEnum(((System.Int64)v), new string[] {@"Tls", @"Tls11", @"Tls12"}, new System.Int64[] {(long)global:: System.Net.SecurityProtocolType.@Tls , (long)global:: System.Net.SecurityProtocolType.@Tls11 , (long)global:: System.Net.SecurityProtocolType.@Tls12 }, @"System.Net.SecurityProtocolType"); break; } return s; } 

You may notice that the code for the Ssl3 enum element is not generated in the event of a failure. You do not know why this code is missing?

+5
source share
1 answer

A simple diagnosis is that SGen.exe started with incorrect referenced assemblies. The /reference command line options matter if they are not used, then they return to those in the c: \ windows \ microsoft.net directory

Suitable for shoes, the SecurityProtocolType enumeration is rather inconsistent. It has been changed from .NET 4.0 to 4.5, adding members to the Tls11 and Tls12 enumerations. There are a lot of Q + A here to include them even in a project that targets 4.0. And there is a pretty big problem with SSLv3, it was compromised after the repair . Accordingly, Microsoft is trying to stop programmers from using it, which you can see in the .NETCore ad . The [Deprecated] attribute is the one that SGen.exe pays attention to.

It’s not entirely clear to me what flavor of the reference assembly could be used. Although SslProtocols.Ssl3 has the [Deprecated] attribute in .NETCore, the SecurityProtocolType enumeration member is not . Therefore, SGen.exe accidentally uses the assembly link. NETCore does not explain this. Something like Mono or Hamarin doesn't just explain it. Silverlight or the PCL reference build do not explain this, at least on my machine. Available beta is always possible.

If you have a known reason to use the unusual /reference argument for SGen.exe, it is best to reinstall the .NET Framework on this computer. And stop using Ssl3, it's really out of date.

+3
source

Source: https://habr.com/ru/post/1263334/


All Articles