gpt4 book ai didi

azure - 使用 Azure 搜索 .NET 检查索引是否存在时出现 NullReferenceException?

转载 作者:行者123 更新时间:2023-12-01 13:27:32 25 4
gpt4 key购买 nike

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

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

使用 postman 的 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 的实例。为什么不抛出?

最佳答案

嗯,这很奇怪,但可能是 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));
}

关于azure - 使用 Azure 搜索 .NET 检查索引是否存在时出现 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com