added keyed singleton for muilti instances
All checks were successful
Publish NuGets / build (release) Successful in 21s
All checks were successful
Publish NuGets / build (release) Successful in 21s
This commit is contained in:
parent
560f562205
commit
6dccfa0bca
@ -10,34 +10,35 @@ namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
private static bool _defaultRegistered = false;
|
||||
|
||||
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
||||
public static IServiceCollection AddJwtTokenManager(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string sectionName,
|
||||
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||
string? name = null,
|
||||
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||
{
|
||||
if (configuration == null)
|
||||
throw new ArgumentNullException(nameof(configuration));
|
||||
if (string.IsNullOrWhiteSpace(sectionName))
|
||||
throw new ArgumentException("Section name must be provided.", nameof(sectionName));
|
||||
|
||||
services.Configure<JwtTokenManagerOptions>(configuration.GetSection(sectionName));
|
||||
|
||||
// Apply the builder options
|
||||
name ??= sectionName;
|
||||
|
||||
services.Configure<JwtTokenManagerOptions>(name, configuration.GetSection(sectionName));
|
||||
|
||||
var builderOptions = new JwtTokenManagerBuilderOptions();
|
||||
configureBuilderOptions?.Invoke(builderOptions);
|
||||
|
||||
// Register the service
|
||||
services.AddSingleton<IJwtTokenManagerService>(provider =>
|
||||
services.AddKeyedSingleton<IJwtTokenManagerService>(name, (provider, key) =>
|
||||
{
|
||||
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
|
||||
var options = optionsMonitor.Get(Options.DefaultName);
|
||||
var options = optionsMonitor.Get(name);
|
||||
|
||||
// Apply additional configuration
|
||||
builderOptions.AdditionalConfiguration?.Invoke(options);
|
||||
|
||||
// Configure cache options
|
||||
var cacheOptions = new JwtTokenManagerCacheOptions();
|
||||
builderOptions.CacheOptions?.Invoke(cacheOptions);
|
||||
|
||||
@ -48,6 +49,24 @@ public static class ServiceCollectionExtensions
|
||||
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
|
||||
});
|
||||
|
||||
if (!_defaultRegistered)
|
||||
{
|
||||
services.AddSingleton<IJwtTokenManagerService>(sp =>
|
||||
sp.GetRequiredKeyedService<IJwtTokenManagerService>(name));
|
||||
|
||||
_defaultRegistered = true;
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
||||
public static IServiceCollection AddJwtTokenManager(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string sectionName,
|
||||
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||
{
|
||||
return AddJwtTokenManager(services, configuration, sectionName, null, configureBuilderOptions);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user