Асинхронные поисковые вызовы Azure иногда приводят к исключению из коллекции зависимостей
Недавно я переписал некоторый код на нашем сайте.NET Core 1.1 для асинхронного выполнения нескольких поисковых вызовов Azure. Однако иногда код выдает странную ошибку, которая, похоже, связана со сбором зависимостей. Что мне нужно сделать, чтобы избежать этой ошибки? Есть ли другой способ, которым я должен идти об этой задаче?
Вот что захватывает App Insights:
{"externalId":"62356138","message":"Элемент с тем же ключом уже добавлен.","parsedStack":[{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Культура = нейтральная, PublicKeyToken=7cec85d7bea7798e","method":"System.ThrowHelper.ThrowArgumentException","level":0,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Культура = нейтральный, PublicKeyToken = 7cec85d7bea7798e "," method ":" System.Runtime.CompilerServices.ConditionalWeakTable2.Add","level":1,"line":0},{"assembly":"Microsoft.AI.DependencyCollector, Version=2.4.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.ApplicationInsights.DependencyCollector.Implementation.HttpCoreDiagnosticSourceListener.OnRequest","level":2,"line":0},{"assembly":"Microsoft.AI.DependencyCollector, Version=2.4.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.ApplicationInsights.DependencyCollector.Implementation.HttpCoreDiagnosticSourceListener.OnNext","level":3,"line":0},{"assembly":"System.Diagnostics.DiagnosticSource, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51","method":"System.Diagnostics.DiagnosticListener.Write","level":4,"line":0},{"assembly":"System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.HttpHandlerDiagnosticListenerExtensions.LogHttpRequestCore","level":5,"line":0},{"assembly":"System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.HttpHandlerDiagnosticListenerExtensions.LogHttpRequest","level":6,"line":0},{"assembly":"System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.WinHttpHandler.SendAsync","level":7,"line":0},{"assembly":"System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.HttpClientHandler.SendAsync","level":8,"line":0},{"assembly":"System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.DelegatingHandler.SendAsync","level":9,"line":0},{"assembly":"Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Rest.RetryDelegatingHandler+<>c__DisplayClass11_0+<<SendAsync>b__1>d.MoveNext","level":10,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":11,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":12,"line":0},{"assembly":"Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Rest.RetryDelegatingHandler+<SendAsync>d__11.MoveNext","level":13,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":14,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":15,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1 + ConfiguredTaskAwaiter.GetResult "," level ": 16," line ": 0}, {" assembly ":" System.Net.Http, Version = 4.1.1.2, Culture = нейтральный, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.HttpClient+d__58.MoveNext","level":17,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture= нейтральный, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":18,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Культура = нейтральный, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":19,"line":0},{"assembly":"System.Private.CoreLib, версия = 4.0.0.0, культура = нейтральная, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd","level":20,"line":0},{"assembly":"Microsoft.Azure.Search, версия = 4.0.0.0, культура = нейтральная, PublicKeyToken = 31bf3856ad364e35", "method": "Microsoft.Azure.Search.DocumentsOperations + d__253.MoveNext","level":21,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":22,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":23,"line":0},{"assembly":"Microsoft.Azure.Search, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35","method":"Microsoft.Azure.Search.DocumentsOperationsExtensions+<SuggestAsync>d__21
1.MoveNext "," level ": 24," line ": 0}, {" assembly ":" System.Private.CoreLib, Version = 4.0.0.0, Culture = нейтральный, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":25,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Культура = нейтральная, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":26,"line":0},{"assembly":"Technomic.Core, Version=1.0.0.0, Culture= нейтральный, PublicKeyToken = NULL " "метод":"Technomic.Services.Search.AzureSearchService+d__12.MoveNext","уровень":27,"линии":97,"имя_файла":"C:\Repos\ Воспламенение \Technomic.Core\Services\Search\Services\AzureSearchService.cs"}]," типа ":"System.ArgumentException","Идентификация":"25475753"}
Вот как я устанавливаю асинхронные вызовы:
// Execute searches asynchronously.
var requests = new List<Task<IEnumerable<SearchResult>>>();
requests.Add(SuggestCompanies(searchRequestParams).ContinueWith(x => x.Result as IEnumerable<SearchResult>));
requests.Add(SuggestFoods(searchRequestParams).ContinueWith(x => x.Result as IEnumerable<SearchResult>));
requests.Add(SuggestReports(searchRequestParams).ContinueWith(x => x.Result as IEnumerable<SearchResult>));
requests.Add(SuggestReportCategories(searchRequestParams).ContinueWith(x => x.Result as IEnumerable<SearchResult>));
requests.Add(SuggestNews(searchRequestParams).ContinueWith(x => x.Result as IEnumerable<SearchResult>));
requests.Add(SuggestContacts(searchRequestParams).ContinueWith(x => x.Result as IEnumerable<SearchResult>));
// Merge results when all searches have completed.
var suggestions = requests.SelectMany(x => x.Result);
await Task.WhenAll(requests);
Все методы "Suggest" выглядят примерно так:
private async Task<IEnumerable<CompanySearchResult>> SuggestCompanies(SearchRequestParams searchRequestParams)
{
var companySuggestions = new List<CompanySearchResult>();
SuggestParameters sp = new SuggestParameters()
{
UseFuzzyMatching = searchRequestParams.AllowFuzzySuggestions,
Top = 100,
Filter = filter,
OrderBy = new List<string>() { "country desc", "rank asc" }
};
var asyncResult = await companyIndexClient.Documents.SuggestAsync<CompanySearchResult>(searchRequestParams.SearchExpression, "companySuggester", sp);
var suggestionResults = asyncResult.Results.ToList();
companySuggestions.AddRange(suggestionResults.Select(x => x.Document));
return companySuggestions;
}