c# - 为什么我的 .net 核心 API 取消请求?
问题描述
我有一个循环的 aync 方法:
private Task<HttpResponseMessage> GetResponseMessage(Region region, DateTime startDate, DateTime endDate)
{
var longLatString = $"q={region.LongLat.Lat},{region.LongLat.Long}";
var startDateString = $"{startDateQueryParam}={ConvertDateTimeToApixuQueryString(startDate)}";
var endDateString = $"{endDateQueryParam}={ConvertDateTimeToApixuQueryString(endDate)}";
var url = $"http://api?key={Config.Key}&{longLatString}&{startDateString}&{endDateString}";
return Client.GetAsync(url);
}
然后我接受响应并将其保存到我的 ef 核心数据库中,但是在某些情况下,我收到此异常消息:操作已取消
我真的不明白。这是TCP握手问题吗?
编辑:
对于上下文,我进行了许多这样的调用,将响应传递给写入 db 的方法(这也太慢了,令人难以置信):
private async Task<int> WriteResult(Response apiResponse, Region region)
{
// since context is not thread safe we ensure we have a new one for each insert
// since a .net core app can insert data at the same time from different users different instances of context
// must be thread safe
using (var context = new DalContext(ContextOptions))
{
var batch = new List<HistoricalWeather>();
foreach (var forecast in apiResponse.Forecast.Forecastday)
{
// avoid inserting duplicates
var existingRecord = context.HistoricalWeather
.FirstOrDefault(x => x.RegionId == region.Id &&
IsOnSameDate(x.Date.UtcDateTime, forecast.Date));
if (existingRecord != null)
{
continue;
}
var newHistoricalWeather = new HistoricalWeather
{
RegionId = region.Id,
CelsiusMin = forecast.Day.Mintemp_c,
CelsiusMax = forecast.Day.Maxtemp_c,
CelsiusAverage = forecast.Day.Avgtemp_c,
MaxWindMph = forecast.Day.Maxwind_mph,
PrecipitationMillimeters = forecast.Day.Totalprecip_mm,
AverageHumidity = forecast.Day.Avghumidity,
AverageVisibilityMph = forecast.Day.Avgvis_miles,
UvIndex = forecast.Day.Uv,
Date = new DateTimeOffset(forecast.Date),
Condition = forecast.Day.Condition.Text
};
batch.Add(newHistoricalWeather);
}
context.HistoricalWeather.AddRange(batch);
var inserts = await context.SaveChangesAsync();
return inserts;
}
编辑:我打了 150,000 个电话。我知道这是有问题的,因为我猜这一切都在内存中,甚至在进行保存之前,但这是我试图让它运行得更快的地方......只是我猜我的实际编写代码被阻塞了:/
var dbInserts = await Task.WhenAll(
getTasks // the list of all api get requests
.Select(async x => {
// parsed can be null if get failed
var parsed = await ParseApixuResponse(x.Item1); // readcontentasync and just return the deserialized json
return new Tuple<ApiResult, Region>(parsed, x.Item2);
})
.Select(async x => {
var finishedGet = await x;
if(finishedGet.Item1 == null)
{
return 0;
}
return await writeResult(finishedGet.Item1, finishedGet.Item2);
})
);
解决方案
.net 核心有一个DefaultConnectionLimit
设置,如评论中所回答。
这限制了到特定域的传出连接,以确保不占用所有端口等。
我的并行工作不正确,导致它超过了限制——我读到的所有内容都不应该是 .net 核心上的 2,但确实如此——这导致连接在收到响应之前关闭。
我把它做得更大,并行工作是否正确,再次降低它。
推荐阅读
- amazon-web-services - 是否可以将事件和上下文作为环境变量传递给由云监视事件触发的 ecs 容器
- java - 没有html css标签java的Lucene索引
- python - 将具有任意维数的列表数据转换为多维列表
- java - 如何根据给定的间隔分隔pdf
- vba - 这个字符串中“#”的作用是什么?
- javascript - jquery tabledit 下拉菜单:是否可以从 mysql 数据库中检索值?
- reactjs - graphql 突变有效负载中缺少字段
- powershell - Get-Variable 如何去掉 DOT DOT DOT
- cocoa - pod setup --verbose 不做任何事情
- c++ - 对数组中的数字进行平方,找到总和和最大的数字