You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking at this package: https://github.com/Azure/Microsoft.Azure.StackExchangeRedis, it seems there's now a weird async context expected during construction. For apps using DI, this becomes ... wonky. Can we add an async hook so that the above package can acquire its OAUTH token right before connection?
vargetConnectionMultiplexerTask=async()=>{varconfig=ConfigurationOptions.Parse(config["redisConnectionString"]);config=awaitconfig.ConfigureForAzureWithTokenCredentialAsync(config["principalId"],newDefaultAzureCredential());returnConnectionMultiplexer.Connect(config);};builder.Services.AddSingleton(getConnectionMultiplexerTask);builder.Services.AddSingleton(static sp =>{varconnectionMultiplexerTask=sp.GetRequiredService<Task<ConnectionMultiplexer>>();if(connectionMultiplexerTask.IsCompleted){returnconnectionMultiplexerTask.GetAwaiter().GetResult();}thrownewInvalidOperationException("Oops! don't forget to initialize the config for the connection multiplexer!");})
...app=builder.Build();// initialize the multiplexer in an async contextawaitapp.Services.GetRequiredService<Task<ConnectionMultiplexer>>();// now the following works, so ConnectionMultiplexer can be injected into other classes.app.Services.GetRequiredService<ConnectionMultiplexer>();
There's already a precedent for one of these hooks. I'm thinking if we can add a BeforeConnectAsync to the ConfigurationOption, then the Azure configuration can do its initialization in there and will appear synchronous from the perspective of the DI container, and the hook can be executed during connection which should already be async.
The text was updated successfully, but these errors were encountered:
craigb
changed the title
Expose an async hook so Azure Redis can setup the config right before connection?
Expose an async hook so Azure Redis can setup the config asynchronously right before connection?
Mar 14, 2024
I think the example is a bit more convoluted than normal here because there's an overload in .Connect() and .ConnectAsync() to allow config modification. The equivalent for V2 is more directly:
vargetConnectionMultiplexerTask=async()=>awaitConnectionMultiplexer.ConnectAsync(config["redisConnectionString"], o =>awaito.ConfigureForAzureWithTokenCredentialAsync(config["principalId"],newDefaultAzureCredential()));
The reason we don't expose this is that's an actual async thing, e.g. fetching the MSI token from local or even remote caches, or whatever token source. That token is generally not specific to the SE.Redis client for many scenarios (e.g. managed identity) and we'd want that to blow up outside the connection and all the time taken there attributable and debuggable in that phase, not in a sync-over-async (.Connect() is sync) encapsulation hiding what's going on with a thread block.
I think the real global issue here is: configuring services in .NET isn't allowed to be async, and these things are async. This really needs a more global solution upstream so we're all out of sync-over-async in that context and actually allow the application to progress in startup while we establish connections against async endpoints with relatively high time cost compared to local. That issue is tracked here: dotnet/aspnetcore#24142
Uh oh!
There was an error while loading. Please reload this page.
Looking at this package: https://github.com/Azure/Microsoft.Azure.StackExchangeRedis, it seems there's now a weird async context expected during construction. For apps using DI, this becomes ... wonky. Can we add an async hook so that the above package can acquire its OAUTH token right before connection?
Right now, people may be doing:
For folks trying to use v2 of the https://github.com/Azure/Microsoft.Azure.StackExchangeRedis package, this becomes something like:
There's already a precedent for one of these hooks. I'm thinking if we can add a
BeforeConnectAsync
to the ConfigurationOption, then the Azure configuration can do its initialization in there and will appear synchronous from the perspective of the DI container, and the hook can be executed during connection which should already be async.The text was updated successfully, but these errors were encountered: