Azure.Search.Documents 将 CORS 添加到索引时抛出错误(以编程方式)
Azure.Search.Documents throws error while adding CORS to the Index (Programmatically)
我正在使用 Azure 认知搜索来索引我的数据。我已经升级到最新的 Azure.Search.Documents(版本 11)包。以前我用的是 Microsoft.Azure.Search
当我以编程方式添加 CORS 时(第 5 行),出现以下错误。
Console.WriteLine("Creating Index...");
FieldBuilder fieldBuilder = new FieldBuilder();
var searchFields = fieldBuilder.Build(typeof(Hotel));
var searchIndex = new SearchIndex("hotels", searchFields);
searchIndex.CorsOptions = new CorsOptions(new List<string> { "*" });
错误:
System.InvalidOperationException
HResult=0x80131509
Message=The requested operation requires an element of type 'Number', but the target element has type
'Null'.
Source=System.Text.Json
StackTrace:
at System.Text.Json.JsonDocument.TryGetValue(Int32 index, Int64& value)
at System.Text.Json.JsonElement.GetInt64()
at Azure.Search.Documents.Indexes.Models.CorsOptions.DeserializeCorsOptions(JsonElement element)
at Azure.Search.Documents.Indexes.Models.SearchIndex.DeserializeSearchIndex(JsonElement element)
at Azure.Search.Documents.IndexesRestClient.CreateOrUpdate(String indexName, SearchIndex index,
Nullable`1 allowIndexDowntime, String ifMatch, String ifNoneMatch, CancellationToken
cancellationToken)
at Azure.Search.Documents.Indexes.SearchIndexClient.CreateOrUpdateIndex(SearchIndex index, Boolean
allowIndexDowntime, Boolean onlyIfUnchanged, CancellationToken cancellationToken)
at AzureSearch.SDKHowTo.Program.<Main>d__0.MoveNext() in C:\Users\dk25\Downloads\search-dotnet-
getting-started-master\search-dotnet-getting-started-master\DotNetHowToIndexers\Program.cs:line 56
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at AzureSearch.SDKHowTo.Program.<Main>(String[] args)
This exception was originally thrown at this call stack:
[External Code]
AzureSearch.SDKHowTo.Program.Main(string[]) in Program.cs
[External Code]
因此,根据我的反复试验方法,我发现您必须设置 MaxAgeInSeconds
对适用于我的代码稍作修改:
var cors_option = new CorsOptions(new List<string> { "*" });
cors_option.MaxAgeInSeconds = 300;
searchIndex.CorsOptions = cors_option;
当我对 MaxAgeInSeconds
发表评论时,出现了与您相同的错误:
我正在使用 Azure 认知搜索来索引我的数据。我已经升级到最新的 Azure.Search.Documents(版本 11)包。以前我用的是 Microsoft.Azure.Search
当我以编程方式添加 CORS 时(第 5 行),出现以下错误。
Console.WriteLine("Creating Index...");
FieldBuilder fieldBuilder = new FieldBuilder();
var searchFields = fieldBuilder.Build(typeof(Hotel));
var searchIndex = new SearchIndex("hotels", searchFields);
searchIndex.CorsOptions = new CorsOptions(new List<string> { "*" });
错误:
System.InvalidOperationException
HResult=0x80131509
Message=The requested operation requires an element of type 'Number', but the target element has type
'Null'.
Source=System.Text.Json
StackTrace:
at System.Text.Json.JsonDocument.TryGetValue(Int32 index, Int64& value)
at System.Text.Json.JsonElement.GetInt64()
at Azure.Search.Documents.Indexes.Models.CorsOptions.DeserializeCorsOptions(JsonElement element)
at Azure.Search.Documents.Indexes.Models.SearchIndex.DeserializeSearchIndex(JsonElement element)
at Azure.Search.Documents.IndexesRestClient.CreateOrUpdate(String indexName, SearchIndex index,
Nullable`1 allowIndexDowntime, String ifMatch, String ifNoneMatch, CancellationToken
cancellationToken)
at Azure.Search.Documents.Indexes.SearchIndexClient.CreateOrUpdateIndex(SearchIndex index, Boolean
allowIndexDowntime, Boolean onlyIfUnchanged, CancellationToken cancellationToken)
at AzureSearch.SDKHowTo.Program.<Main>d__0.MoveNext() in C:\Users\dk25\Downloads\search-dotnet-
getting-started-master\search-dotnet-getting-started-master\DotNetHowToIndexers\Program.cs:line 56
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at AzureSearch.SDKHowTo.Program.<Main>(String[] args)
This exception was originally thrown at this call stack:
[External Code]
AzureSearch.SDKHowTo.Program.Main(string[]) in Program.cs
[External Code]
因此,根据我的反复试验方法,我发现您必须设置 MaxAgeInSeconds
对适用于我的代码稍作修改:
var cors_option = new CorsOptions(new List<string> { "*" });
cors_option.MaxAgeInSeconds = 300;
searchIndex.CorsOptions = cors_option;
当我对 MaxAgeInSeconds
发表评论时,出现了与您相同的错误: