### Install Algolia.Search via .NET CLI Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/README.md Use this command to install the Algolia.Search NuGet package from the .NET CLI. Replace with the desired version number. ```bash dotnet add package Algolia.Search --version ``` -------------------------------- ### Initialize client and save/search records Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/README.md Shows how to import the Algolia client, create a SearchClient, save an object, wait for indexing, and perform a search with typo tolerance. Replace YOUR_APP_ID, YOUR_API_KEY, YOUR_INDEX_NAME, and YOUR_QUERY with your actual values. ```csharp using Algolia.Search.Clients; using Algolia.Search.Http; var client = new SearchClient(new SearchConfig("YOUR_APP_ID", "YOUR_API_KEY")); // Add a new record to your Algolia index var response = await client.SaveObjectAsync( "", new Dictionary { { "objectID", "id" }, { "test", "val" } } ); // Poll the task status to know when it has been indexed await client.WaitForTaskAsync("", response.TaskID); // Fetch search results, with typo tolerance var response = await client.SearchAsync( new SearchMethodParams { Requests = new List { new SearchQuery( new SearchForHits { IndexName = "", Query = "", HitsPerPage = 50, } ) }, } ); ``` -------------------------------- ### Build and Test Commands Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Commands to build, generate CTS tests, run tests, and format the C# client. Run from repo root or client directory. ```bash # From repo root (api-clients-automation) yarn cli build clients csharp # Build C# client yarn cli cts generate csharp # Generate CTS tests yarn cli cts run csharp # Run CTS tests yarn cli format csharp clients/algoliasearch-client-csharp # From client directory cd clients/algoliasearch-client-csharp dotnet build # Build solution dotnet test # Run tests dotnet format # Format code ``` -------------------------------- ### Add Algolia.Search PackageReference to .csproj Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/README.md Add this PackageReference to your .csproj file to reference the Algolia.Search package. The Version attribute uses a placeholder that should be replaced with the actual version. ```csharp ``` -------------------------------- ### LINQ Usage Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Shows how to use LINQ to filter and project collections from response.Hits. ```csharp // Use LINQ for collections var filtered = response.Hits .Where(h => h.Score > 0.5) .Select(h => h.ObjectID) .ToList(); ``` -------------------------------- ### Async/Await Patterns Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Shows correct and incorrect ways to handle async operations. Avoid blocking with .Result due to deadlock risk; use await or GetAwaiter().GetResult() if blocking is unavoidable. ```csharp // WRONG - blocking on async var response = client.SearchAsync(params).Result; // Deadlock risk! // CORRECT - await properly var response = await client.SearchAsync(params); // If you must block (avoid), use var response = client.SearchAsync(params).GetAwaiter().GetResult(); ``` -------------------------------- ### Disposal Patterns Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Shows two ways to dispose the SearchClient: using statement or explicit try/finally. Both ensure proper resource cleanup. ```csharp // Clients are disposable - use using statement using var client = new SearchClient("APP_ID", "API_KEY"); await client.SearchAsync(params); // Or explicit disposal var client = new SearchClient("APP_ID", "API_KEY"); try { await client.SearchAsync(params); } finally { client.Dispose(); } ``` -------------------------------- ### Serialization with System.Text.Json Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Shows how to annotate model properties with JsonPropertyName for System.Text.Json serialization. Custom converters are placed in Serializer/. ```csharp // Models use System.Text.Json [JsonPropertyName("objectID")] public string ObjectId { get; set; } // Custom converters in Serializer/ ``` -------------------------------- ### Async API method with cancellation token Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Shows the async method signature and usage with and without a cancellation token. All API methods have async versions. ```csharp // All API methods have async versions public async Task SearchAsync(SearchParams searchParams) // Usage var response = await client.SearchAsync(searchParams); // With cancellation var response = await client.SearchAsync(searchParams, cancellationToken); ``` -------------------------------- ### Nullable Reference Types Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Demonstrates C# 8+ nullable annotations, null-conditional, null-coalescing, and null-forgiving operators. Use nullable! sparingly. ```csharp // C# 8+ nullable annotations string? nullable = null; // Can be null string nonNull = ""; // Cannot be null // Null-conditional var length = nullable?.Length; // Null-coalescing var value = nullable ?? "default"; // Null-forgiving (use sparingly) var sure = nullable!; ``` -------------------------------- ### Check Git Remote Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Command to verify the current repository's remote URL. Use this before making any changes to ensure you are in the correct repository (api-clients-automation) and not the public algoliasearch-client-csharp repo. ```bash git remote -v ``` -------------------------------- ### Generation Config Patterns Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md JavaScript code from config/generation.config.mjs showing which file patterns are generated (without '!') and which are hand-written (with '!'). Use this to determine if a file is safe to edit. ```javascript // In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) 'clients/algoliasearch-client-csharp/**', // Generated by default '!clients/algoliasearch-client-csharp/algoliasearch/Clients/AlgoliaConfig.cs', // Hand-written ✓ '!clients/algoliasearch-client-csharp/algoliasearch/Exceptions/**', // Hand-written ✓ '!clients/algoliasearch-client-csharp/algoliasearch/Serializer/**', // Hand-written ✓ '!clients/algoliasearch-client-csharp/algoliasearch/Utils/**', // Hand-written ✓ '!clients/algoliasearch-client-csharp/algoliasearch/Http/**', // Hand-written ✓ '!clients/algoliasearch-client-csharp/algoliasearch/Transport/**', // Hand-written ✓ '!clients/algoliasearch-client-csharp/algoliasearch/Models/Common/**', // Hand-written ✓ ``` -------------------------------- ### Exception hierarchy for Algolia errors Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Defines the exception classes used for Algolia-specific errors. AlgoliaApiException includes HTTP status code and body; AlgoliaUnreachableHostsException indicates host unreachable. ```csharp // Exceptions/ public class AlgoliaException : Exception { } public class AlgoliaApiException : AlgoliaException { public int HttpStatusCode { get; } public string HttpBody { get; } } public class AlgoliaUnreachableHostsException : AlgoliaException { } ``` -------------------------------- ### HttpTransport class with retry logic Source: https://github.com/algolia/algoliasearch-client-csharp/blob/main/AGENTS.md Defines the HTTP transport layer with retry and host failover. Use this as the base for making HTTP requests in the client. ```csharp // Transport/ - HTTP layer with retry public class HttpTransport : ITransport { private readonly HttpClient _httpClient; private readonly RetryStrategy _retryStrategy; public async Task ExecuteAsync(HttpRequest request) { // Retry logic with host failover } } ```