【发布时间】:2019-07-02 05:35:30
【问题描述】:
我有一个由 App Service plan(S3) 托管的 blob 触发的 azure 函数 (v2)。该函数处理一个json文件,并将Http calls提供给API管理服务中暴露的API。
我正在使用HttpClient 拨打Http 电话。尽管这在具有相同设置的一个环境中运行良好,但在另一个环境中进行 http post 调用时却失败了。
例外:
System.Net.Sockets.SocketException带有错误消息 “操作已取消。无法从传输连接读取数据:由于线程退出或应用程序请求,I/O 操作已中止。由于线程退出或应用程序请求,I/O 操作已中止。申请要求”
快速谷歌搜索告诉我,这可能是由于过多的 http 连接而发生的。更多细节在这里:
https://docs.microsoft.com/en-us/azure/azure-functions/manage-connections#connections-limit
解决方案是使用静态HttpClient 或横向扩展应用服务计划。我做了这两个只是为了意识到它仍然失败并出现同样的异常。
有人遇到过这个问题吗?
任何见解都会有所帮助。
编辑:这是进行 http 调用的代码
public class ReportingService : IReportingService
{
private static readonly HttpClient _httpClient = new HttpClient();
private readonly ILogger _logger;
public ReportingService(ILogger<ReportingService> logger, IConfigurationRoot configuration)
{
_logger = logger;
_httpClient.BaseAddress = new Uri(configuration["ReportingServiceBaseUrl"]);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{configuration["APIUser"]}:{configuration["APIPassword"]}")));
}
public async Task<bool> RequestReport(string endpoint, StringContent httpContent)
{
try
{
var response = await _httpClient.PostAsync(endpoint, httpContent);
return response.IsSuccessStatusCode;
}
catch (Exception ex)
{
_logger.LogError(ex, "Migration failed");
throw;
}
}
}
【问题讨论】:
-
如果你能发布一个 minimal reproducible example 的代码,那将是非常棒的
-
ReportingService(单音等)有什么生命周期范围?
-
@vladimir 是的,它是单身
-
所以它在一个环境中工作而在另一个环境中失败。那么问题不必出在您的代码中。看看环境的差异。例如,非工作环境是否有防火墙?
-
@PNDev 你能评论一下你是如何解决这个问题的吗?
标签: c# .net http azure-functions