检查Azure Search for .NET是否存在索引时出现NullReferenceException?

时间:2017-12-11 12:59:24

标签: azure azure-search azure-search-.net-sdk

当试图查看使用.NET SDK(3.0.4和4.0.0预览版)的不存在索引的索引是否存在ExistsAsync(以及Exists时,并且ExistsWithHttpMessagesAsync)抛出以下异常。

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Azure.Search.IndexesOperations.<GetWithHttpMessagesAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Search.ExistsHelper.<ExistsFromGetResponse>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Search.IndexesOperationsExtensions.<ExistsAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at InphizCore.MiddleLayer.Services.AzureSearch.Services.AzureSearchWriter`1.<AssertIndexExists>d__8.MoveNext() in C:\xxx\AzureSearchWriter.cs:line 109

使用邮递员的http rest工作正常,并返回一条消息,指出索引不存在。

 public async Task AssertIndexExists()
        {
            try
            {
                if (await searchServiceClient.Indexes.ExistsAsync(options.Value.IndexName) == false)
                {
                    searchServiceClient.Indexes.Create(new Index(options.Value.IndexName, FieldBuilder.BuildForType<SearchableItemModel>(), corsOptions: new CorsOptions(new List<string> { "*" })));
                }
            }
            catch (Exception e)
            {
                logger.LogError($"Azure Search Index '{options.Value.IndexName}' could not be created. ({e.Message})");
                throw e;
            }
        }

如何以有意义的方式解决此问题?

更新:

这是客户在单元测试中运行时的外观:

enter image description here

这是从AspNetCore MVC看起来的样子:

enter image description here

似乎搜索客户端无法创建FirstMessageHandlerHttpClientHttpClientHandler的实例。为什么不扔?

1 个答案:

答案 0 :(得分:3)

嗯,这很奇怪,但可能是SearchServiceClient的一些内部工作。

这(编辑:没有)工作:

如果我添加HttpClientHandler我传递给另一个构造函数并将生命周期设置为SingletonTransient,则可以正常工作。我之前有Scoped.

 services.AddTransient<SearchServiceClient>(x =>
 {
     var httpClientHandler = new HttpClientHandler();

     var options = x.GetRequiredService<IOptions<AzureSearchOptions>>();
     var client = new SearchServiceClient(options.Value.SearchServiceName, new SearchCredentials(options.Value.AdminApiKey), httpClientHandler);
     return client;
 });

更新:此方法有效:

上面的解决方案中的行为是不可预测的,我最终在调用类中使用了这个方法。

当我每次需要一个客户端创建一个新实例时,它似乎根本没有给出任何错误:

  protected SearchServiceClient GetAzureSearchServiceClient()
        {
            return new SearchServiceClient(options.Value.SearchServiceName, new SearchCredentials(options.Value.AdminApiKey));
        }