我有两个项目使用的.netstandard2.0项目.一个是. net核心项目,一个是. netframework4.7.1.当我在.netframework4.7.1中引用我的.Netstandard2.0项目时,我得到了HttpClient.sslProtocol的"PlatformNotSupportedException".我知道在.netframework4.7.1中不支持它,但我想跳过代码.netframework4.7.1,不要跳过. netcore.我如何在.netstansard2.0中处理这个问题? 我无法访问.netframework4.7.1和. net核心.我只能访问.netstandard2.0,其中"HttpClient.sslProtocol"使用.我可以在.netframework4.7.1和. net core中做任何事情.我可以在.netstandard2.0中做什么? 在. netcore.netcore.com中运行良好.
这是我的密码
private static readonly HttpClient _httpClient = new HttpClient(new HttpClientHandler() {
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
ServerCertificateCustomValidationCallback = null,
SslProtocols = System.Security.Authentication.SslProtocols.Tls11
})
{
Timeout = TimeSpan.FromSeconds(45)
};
以下代码是实现ihttpclientfactory时的代码
private static readonly Lazy<IHttpClientFactory> httpClientFactoryLazy = new Lazy<IHttpClientFactory>(() =>
{
var services = new ServiceCollection();
services.AddHttpClient("HttpClientFactory", client =>
{
client.Timeout = TimeSpan.FromSeconds(15);
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler();
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
return handler;
});
var serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetRequiredService<IHttpClientFactory>();
});
我们可以使用下面的代码识别框架版本并相应地执行代码吗?
private string getCallingFramework() {
return RuntimeInformation.FrameworkDescription;
}