Azure / Microsoft.Azure.StackExchangeRedis

Azure-specific wrapper for the StackExchange.Redis client library
MIT License
17 stars 14 forks source link

Proposal: Adding IServiceProvider to ConnectionFactory #45

Closed joacar closed 6 months ago

joacar commented 6 months ago

Hi

This question is based is my assumption that the IConnectionInstance should be singleton and that the underlaying connection and authentication is managed by this package.

Following that, would it be possible to add IServiceProivder to ConmectionFactory? Currently I’ve implemented a health check that runs on container startup and I would like to reuse that connection since it’s already authenticated. The health check retrieves singleton from service container and binds option to configuration.

Edit: I could of course use the options class in my health check if it (ConnectionFactory) is called once. I don’t think the breakpoint was hit multiple times inside the delegate.

best regards

philon-msft commented 6 months ago

This package doesn't actually create or manage a Redis connection, it just customizes a ConfigurationOptions instance that your app can use to create a Redis connection.

It sounds like you're using a dependency injection system. To add a singleton ConnectionMultiplexer instance you'd use something like this:

var redisConfig = ConfigurationOptions.Parse(redisConnectionString);
await redisConfig.ConfigureForAzureWithSystemAssignedManagedIdentityAsync(principalId);
services.AddSingleton<IConnectionMultiplexer>(await ConnectionMultiplexer.ConnectAsync(redisConfig));
joacar commented 6 months ago

Will that instance be used then if I don’t specify neither ConnectionString nor ConnectionMultiplexerFactory during service registration?

Thanks

fre 3 maj 2024 kl. 17:31 skrev Philo @.***>:

This package doesn't actually create or manage a Redis connection, it just customizes a ConfigurationOptions instance that your app can use to create a Redis connection.

It sounds like you're using a dependency injection system. To add a singleton ConnectionMultiplexer instance you'd use something like this:

var redisConfig = ConfigurationOptions.Parse(redisConnectionString);await redisConfig.ConfigureForAzureWithSystemAssignedManagedIdentityAsync(principalId); services.AddSingleton(await ConnectionMultiplexer.ConnectAsync(redisConfig));

— Reply to this email directly, view it on GitHub https://github.com/Azure/Microsoft.Azure.StackExchangeRedis/issues/45#issuecomment-2093249060, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADLF5FYDMXMHA7DXNHXCPTZAOUU3AVCNFSM6AAAAABHDZEXWWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJTGI2DSMBWGA . You are receiving this because you authored the thread.Message ID: @.***>

philon-msft commented 6 months ago

A connection string is passed into the ConfigurationOptions.Parse() call. The single instance created and registered via AddSingleton() in that sample will be used throughout your application.

philon-msft commented 6 months ago

Closing this now to tidy up. Feel free to reactivate if needed.