C# 间歇性错误:请求被中止:无法创建SSL/TLS安全通道

C# 间歇性错误:请求被中止:无法创建SSL/TLS安全通道,c#,client,.net-4.5,tls1.2,windows-server-2012-r2,C#,Client,.net 4.5,Tls1.2,Windows Server 2012 R2,我们使用C#.net 4.5控制台应用程序编写的客户端来使用Web服务(我们的Web服务支持TLS,不支持SSL)。99%的工作时间没有任何问题,但有时我们会收到错误消息“请求被中止:无法以随机方式创建SSL/TLS安全通道”。在几毫秒内,它也将恢复正常。客户端机器的操作系统是Windows server 2012 r2。 同一应用程序运行时没有出现Windows 10中的任何问题 static async Task<string> CheckStudentStatus(string

我们使用C#.net 4.5控制台应用程序编写的客户端来使用Web服务(我们的Web服务支持TLS,不支持SSL)。99%的工作时间没有任何问题,但有时我们会收到错误消息“请求被中止:无法以随机方式创建SSL/TLS安全通道”。在几毫秒内,它也将恢复正常。客户端机器的操作系统是Windows server 2012 r2。 同一应用程序运行时没有出现Windows 10中的任何问题

static async Task<string> CheckStudentStatus(string studentID)
        {
            string studentstat = "error";
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            try
            {

                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://EndpointURL" + studentID))
                    {

                        request.Headers.TryAddWithoutValidation("Accept", "application/json");
                        request.Headers.TryAddWithoutValidation("Connection", "Keep-Alive");
                        request.Headers.TryAddWithoutValidation("Authorization", "Bearer AuthenticationToken");

                        var response = await httpClient.SendAsync(request);
                        var responseJson = System.Threading.Tasks.Task.Run(async () => await response.Content.ReadAsStringAsync())
                   .GetAwaiter().GetResult();
                        Console.WriteLine(responseJson.ToString());
                        if ((int)response.StatusCode == 200)
                        {
                            JObject joResponse = JObject.Parse(responseJson);
                            var studentStatus = joResponse.GetValue("StudentStatus");
                            string Stat = studentStatus?.ToString();
                            if (!string.IsNullOrWhiteSpace((Stat)))
                            {
                                studentstat = Stat;
                            }
                        }
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine("Error message: " + ex.Message);
                Console.WriteLine("Stack Trace " + ex.StackTrace);
                Console.WriteLine("Inner exception " + ex.InnerException.Message);
            }
            return studentstat;
        }

静态异步任务CheckStudentStatus(字符串studentID)
{
字符串studentstat=“error”;
ServicePointManager.Expect100Continue=true;
ServicePointManager.ServerCertificateValidationCallback+=(发件人、证书、链、错误)=>true;
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;
尝试
{
使用(var httpClient=new httpClient())
{
使用(var request=newhttprequestmessage(newhttpmethod(“GET”),”https://EndpointURL“+学生ID))
{
request.Headers.TryAddWithoutValidation(“Accept”、“application/json”);
request.Headers.TryAddWithoutValidation(“连接”,“保持活动”);
request.Headers.tryadd不带验证(“授权”、“承载认证令牌”);
var response=wait httpClient.sendaync(请求);
var responseJson=System.Threading.Tasks.Task.Run(异步()=>wait-response.Content.ReadAsStringAsync())
.GetAwaiter().GetResult();
Console.WriteLine(responseJson.ToString());
如果((int)response.StatusCode==200)
{
JObject joResponse=JObject.Parse(responseJson);
var studentStatus=joResponse.GetValue(“studentStatus”);
字符串Stat=studentStatus?.ToString();
如果(!string.IsNullOrWhiteSpace((Stat)))
{
studentstat=Stat;
}
}
}
}
}
捕获(HttpRequestException-ex)
{
Console.WriteLine(“错误消息:+ex.message”);
Console.WriteLine(“堆栈跟踪”+ex.StackTrace);
Console.WriteLine(“内部异常”+ex.InnerException.Message);
}
返回studentstat;
}

处理任何http调用的责任在于使用重试策略。这是一个很好的框架。另外,但可能不相关-不要为每个请求创建一个新的HttpClient。这意味着可以重复使用。不要使用Task读取响应内容。运行,然后使用GetResult阻止。只需删除该任务。完全运行。这浪费了线程池线程。通过添加自定义重试处理程序解决了此问题。