Home Asynchronous Azure search calls sometimes result in dependency collection exception
Reply: 0

Asynchronous Azure search calls sometimes result in dependency collection exception

user8273
1#
user8273 Published in May 21, 2018, 9:05 am

I recently rewrote some code on our .NET Core 1.1 site to make several Azure search calls asynchronously. However, sometimes the code throws a strange error which seems to be related to dependency collection. What do I need to do to avoid this error? Is there another way I should be going about this task?

Here is what App Insights is capturing:

{"outerId":"62356138","message":"An item with the same key has already been added.","parsedStack":[{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.ThrowHelper.ThrowArgumentException","level":0,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, 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.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter.GetResult","level":16,"line":0},{"assembly":"System.Net.Http, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a","method":"System.Net.Http.HttpClient+d__58.MoveNext","level":17,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":18,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":19,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd","level":20,"line":0},{"assembly":"Microsoft.Azure.Search, Version=4.0.0.0, Culture=neutral, 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__211.MoveNext","level":24,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":25,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":26,"line":0},{"assembly":"Technomic.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"Technomic.Services.Search.AzureSearchService+d__12.MoveNext","level":27,"line":97,"fileName":"C:\Repos\Ignite\Technomic.Core\Services\Search\Services\AzureSearchService.cs"}],"type":"System.ArgumentException","id":"25475753"}

Here is how I set up the asynchronous calls:

// 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);

The "Suggest" methods all look something like this:

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;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.464433 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO