为什么我的.NET Core 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核心数据库中,但是在某些情况下,我会得到此例外消息:Operaiton已取消

我真的不明白。这是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;
            }

编辑:我正在拨打15万个电话。我知道这是值得怀疑的,因为这一切都在记忆中,所以我猜在保存之前,这是我尝试更快地运行的地方...只有我猜我的实际写作代码是阻止的:/

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 core具有评论中回答的DefaultConnectionLimit设置。

这将传出连接限制到特定域,以确保未接收所有端口等。

我做了我的并行工作,导致它超过了极限 - 我读过的一切都不应在.NET核心上是2,但这是在收到响应之前关闭的连接。

我做得更大,并平行起作用,再次降低了它。

最新更新