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

检查Azure Search for.NET是否存在索引时出现NullReferenceException?,azure,azure-cognitive-search,azure-search-.net-sdk,Azure,Azure Cognitive Search,Azure Search .net Sdk,尝试使用.NET SDK(3.0.4和4.0.0-preview)查看不存在的索引是否存在时,ExistsAsync(以及exists,和existswithhttpmessageasync)抛出以下异常 System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Azure.Search.IndexesOperations.<GetWithHttp

尝试使用.NET SDK(3.0.4和4.0.0-preview)查看不存在的索引是否存在时,
ExistsAsync
(以及
exists
,和
existswithhttpmessageasync
)抛出以下异常

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
System.NullReferenceException:对象引用未设置为对象的实例。
在Microsoft.Azure.Search.IndexesOperations.d_u12.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.Azure.Search.ExistsHelper.d_u0`1.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.Azure.Search.IndexeSoperationExtensions.d_u3.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在InHizCore.MiddleLayer.Services.AzureSearch.Services.AzureSearchWriter`1

以下是AspNetCore MVC的外观:


搜索客户端似乎无法创建
FirstMessageHandler
HttpClient
HttpClientHandler
的实例。为什么不抛出?

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

此(编辑:不起作用):

如果我添加一个
HttpClientHandler
,我将它传递给另一个构造函数,并将生存期设置为
Singleton
Transient
,它就会工作。我以前有
范围。

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

你介意为此在GitHub上打开一个问题吗?回购协议如下:请将[Azure Search SDK]放在标题开头。谢谢当然创建了这个Hi Jonas,我有一个关于导致NullReferenceException的假设。有关详细信息,请参阅GitHub问题。
 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));
        }