Asp.Net 核心:调用外部 API 时的 HttpRequestException



我在从我的.NET Core 1.1 Web应用程序使用HTTPS(TLS 1.2(对外部API进行HTTP调用时遇到问题。应用程序在本地运行良好,但在部署到 Azure 应用服务时出现以下异常:

exceptionMessage:An error occurred while sending the request.
exceptionType:System.Net.Http.HttpRequestException

内部例外是:

innerExceptionMessage:A security error occurred
innerExceptionType:System.Net.Http.WinHttpException

奇怪的是,我没有在对外部 API 的所有请求上都出现此异常,它只发生几次。

我注意到在其他 SO 帖子中也讨论了类似的问题,但其中大多数都是关于自签名证书的。就我而言,远程服务器正在使用由GeoTrust签名的证书。

所有呼叫都使用System.Net.Http.HttpClient

相关:

  • Asp.Net 核心:调用另一个 REST-API 会引发 WebException
  • .net core API Post 异常给出 NativeErrorCode 12175
  • 使用 HttpClient 的 HTTPS 请求失败
  • 安全错误,在 Azure(Asp.net 核心(上使用客户端证书(自签名根(发布

全栈跟踪:

{ exceptionType: "System.Net.Http.HttpRequestException", innerException: { innerStacktrace: " at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()", innerExceptionMessage: "A security error occurred", innerExceptionType: "System.Net.Http.WinHttpException" }, stacktrace: " at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Integration.Services.ApiService.<PostResourceToApiAsync>d__5`1.MoveNext() in C:projectsintegrationsrcIntegrationServicesApiService.cs:line 112 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Integration.Services.AccountService.<NotifyOwnerAsync>d__12.MoveNext() in C:projectsintegrationsrcIntegrationServicesAccountService.cs:line 115 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Integration.Services.AccountService.<CreateAccountAsync>d__9.MoveNext() in C:projectsintegrationsrcIntegrationServicesAccountService.cs:line 66 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Integration.Services.PartnerService.<CreateAccountAsync>d__11.MoveNext() in C:projectsintegrationsrcIntegrationServicesPartnerService.cs:line 100 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Integration.Controllers.AccountsController.<Create>d__7.MoveNext() in C:projectsintegrationsrcIntegrationControllersAccountsController.cs:line 92 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__25.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextExceptionFilterAsync>d__24.MoveNext()", exceptionMessage: "An error occurred while sending the request." }

我认为我看到的问题是由于这个错误:https://github.com/dotnet/corefx/issues/7812

这就解释了为什么我只在将应用程序部署到Azure中的Web appp时才看到这一点,因为它运行的是Windows Server 2012。真正的解决方法是升级到Windows Server 2016,但目前Azure中的Web应用程序不支持:-(

我现在的解决方法只是一个简单的重试机制,似乎运行良好。

最新更新