skip to Main Content

I am trying to replace Azure.Search to Azure.Search.Document.

Here i am finding a issue in the GetIndexesAsync Method , where this method requires a cancellation token.

I just want how this need to be implemented so that we can get that token and proceed further.

Please help.

I tried implementing the Cancellation token but it didn’t work for me .
need help to understand its exact way to implement.

2

Answers


  1. I tried a sample C# console code with Azure.Search.Documents package to retrieve Azure Search Indexes using a cancellation token.

    Code :

    using Azure;
    using Azure.Search.Documents;
    using Azure.Search.Documents.Indexes;
    using Azure.Search.Documents.Indexes.Models;
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            string searchServiceEndpoint = "<SearchService_Endpoint>";
            string adminApiKey = "<API_admin_key>";
    
            Uri serviceEndpoint = new Uri(searchServiceEndpoint);
            AzureKeyCredential credential = new AzureKeyCredential(adminApiKey);
            SearchIndexClient indexClient = new SearchIndexClient(serviceEndpoint, credential);
    
            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                try
                {
                    cts.CancelAfter(TimeSpan.FromSeconds(30));
    
                    await GetIndexesAsync(indexClient, cts.Token);
                }
                catch (TaskCanceledException)
                {
                    Console.WriteLine("Operation was canceled.");
                }
                catch (RequestFailedException ex)
                {
                    Console.WriteLine($"Error getting indexes: {ex.Message}");
                }
            }
        }
    
        static async Task GetIndexesAsync(SearchIndexClient indexClient, CancellationToken cancellationToken)
        {
            await foreach (Page<SearchIndex> page in indexClient.GetIndexesAsync(cancellationToken: cancellationToken).AsPages())
            {
                foreach (SearchIndex result in page.Values)
                {
                    Console.WriteLine($"Index Name: {result.Name}");
                }
            }
    
            Console.WriteLine("Indexes retrieved successfully!");
        }
    }
    

    Output :

    The code ran successfully and retrieved the index as below.

    enter image description here

    Login or Signup to reply.
  2. The cancellation token parameter of GetIndexesAsync is optional and does not need to be passed if you do not wish to cancel the operation.

    It can be called simply as:

    client.GetIndexesAsync();
    

    In the real world, you’d use it similar to:

    await foreach (var result in client.GetIndexesAsync())
    {
        Console.WriteLine($"Index Name: {result.Name}");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search