/// <inheritdoc /> public CosmosClient CreateCosmosClient(CosmosDataStoreConfiguration configuration) { EnsureArg.IsNotNull(configuration, nameof(configuration)); var host = configuration.Host; var key = configuration.Key; if (string.IsNullOrWhiteSpace(host) && string.IsNullOrWhiteSpace(key)) { _logger.LogWarning("No connection string provided, attempting to connect to local emulator."); host = CosmosDbLocalEmulator.Host; key = CosmosDbLocalEmulator.Key; } _logger.LogInformation("Creating CosmosClient instance for {DatabaseId}, Host: {Host}", configuration.DatabaseId, host); var builder = new CosmosClientBuilder(host, key) .WithConnectionModeDirect(enableTcpConnectionEndpointRediscovery: true) .WithCustomSerializer(new FhirCosmosSerializer()) .WithThrottlingRetryOptions(TimeSpan.FromSeconds(configuration.RetryOptions.MaxWaitTimeInSeconds), configuration.RetryOptions.MaxNumberOfRetries) .AddCustomHandlers(_requestHandlers.ToArray()); if (configuration.PreferredLocations?.Any() == true) { builder.WithApplicationPreferredRegions(configuration.PreferredLocations?.ToArray()); } if (configuration.DefaultConsistencyLevel != null) { builder.WithConsistencyLevel(configuration.DefaultConsistencyLevel.Value); } return(builder.Build()); }