为什么.net核心API取消请求?

时间:2019-02-17 13:07:26

标签: c# .net-core

我有一个循环的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);
                })
            );

1 个答案:

答案 0 :(得分:0)

.net核心具有DefaultConnectionLimit设置,如评论中所述。

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

我的并行工作不正确,导致它超出了限制-我所读的内容在.net内核上不应为2,而是-并且导致连接在收到响应之前关闭。

我加大了它,并行工作正常,然后又降低了。