ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
在.Net 4.5中,SecurityProtocolType枚举有四个成员:
public enum SecurityProtocolType { Ssl3 48,Tls 192,Tls11 768,Tls12 3072 }
但是,在.Net 4.0中,SecurityProtocolType枚举只有两个成员:
public enum SecurityProtocolType { Ssl3 48,Tls 192 }
因为,我的代码中的另一个项目也需要相同的修复,但该项目是在.Net 4.0上没有Tls12作为枚举的成员,this答案提出了以下解决方法(前提是我安装了.Net 4.5)框):
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
可能是我遗漏了明显的smoething,但我的问题是,当3072不是.Net 4.0中枚举的有效值时,(SecurityProtocolType)3072如何解析为Tls12.我想了解幕后工作的魔力是什么.
解决方法
A variable of type Day can be assigned any value in the range of the underlying type; the values are not limited to the named constants.
因此代码肯定没有问题编译.此外:
Just as with any constant,all references to the individual values of an enum are converted to numeric literals at compile time. This can create potential versioning issues as described in Constants.
Assigning additional values to new versions of enums,or changing the values of the enum members in a new version,can cause problems for dependent source code.
你实际上正在利用这一点.在.NET 4.0上运行时,框架不知道如何处理3072值,但.NET 4.5框架可以.您只是没有方便的快捷方式(枚举)来获取该值.