Azure.Search.Documents throws error while adding CORS to the Index (Programmatically)

1

I'm using azure cognitive search to index my data. I've upgraded to the latest Azure.Search.Documents (version 11) package. Previously I was using Microsoft.Azure.Search

When I added CORS programmatically (Line No 5) I get the below error.

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> { "*" });

Error:

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]
c#
azure
azure-cognitive-search

1 Answer

2

So from my trial and error methods, I found that you will have to set the MaxAgeInSeconds

Slight modification to the code which worked for me :

var cors_option = new CorsOptions(new List<string> { "*" });
cors_option.MaxAgeInSeconds = 300;
searchIndex.CorsOptions = cors_option;

When I had commented to the MaxAgeInSeconds, was provided with the same error you were :

enter image description here

answered on Stack Overflow Nov 10, 2020 by Satya V

User contributions licensed under CC BY-SA 3.0