added keyed singleton for muilti instances
This commit is contained in:
parent
560f562205
commit
0a27deef60
@ -10,11 +10,14 @@ namespace OpenHarbor.JwtTokenManager;
|
|||||||
|
|
||||||
public static class ServiceCollectionExtensions
|
public static class ServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
|
private static bool _defaultRegistered = false;
|
||||||
|
|
||||||
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
|
||||||
public static IServiceCollection AddJwtTokenManager(
|
public static IServiceCollection AddJwtTokenManager(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
IConfiguration configuration,
|
IConfiguration configuration,
|
||||||
string sectionName,
|
string sectionName,
|
||||||
|
string? name = null,
|
||||||
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
Action<JwtTokenManagerBuilderOptions>? configureBuilderOptions = null)
|
||||||
{
|
{
|
||||||
if (configuration == null)
|
if (configuration == null)
|
||||||
@ -22,22 +25,20 @@ public static class ServiceCollectionExtensions
|
|||||||
if (string.IsNullOrWhiteSpace(sectionName))
|
if (string.IsNullOrWhiteSpace(sectionName))
|
||||||
throw new ArgumentException("Section name must be provided.", nameof(sectionName));
|
throw new ArgumentException("Section name must be provided.", nameof(sectionName));
|
||||||
|
|
||||||
services.Configure<JwtTokenManagerOptions>(configuration.GetSection(sectionName));
|
name ??= sectionName;
|
||||||
|
|
||||||
|
services.Configure<JwtTokenManagerOptions>(name, configuration.GetSection(sectionName));
|
||||||
|
|
||||||
// Apply the builder options
|
|
||||||
var builderOptions = new JwtTokenManagerBuilderOptions();
|
var builderOptions = new JwtTokenManagerBuilderOptions();
|
||||||
configureBuilderOptions?.Invoke(builderOptions);
|
configureBuilderOptions?.Invoke(builderOptions);
|
||||||
|
|
||||||
// Register the service
|
services.AddKeyedSingleton<IJwtTokenManagerService>(name, (provider, key) =>
|
||||||
services.AddSingleton<IJwtTokenManagerService>(provider =>
|
|
||||||
{
|
{
|
||||||
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
|
var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
|
||||||
var options = optionsMonitor.Get(Options.DefaultName);
|
var options = optionsMonitor.Get(name);
|
||||||
|
|
||||||
// Apply additional configuration
|
|
||||||
builderOptions.AdditionalConfiguration?.Invoke(options);
|
builderOptions.AdditionalConfiguration?.Invoke(options);
|
||||||
|
|
||||||
// Configure cache options
|
|
||||||
var cacheOptions = new JwtTokenManagerCacheOptions();
|
var cacheOptions = new JwtTokenManagerCacheOptions();
|
||||||
builderOptions.CacheOptions?.Invoke(cacheOptions);
|
builderOptions.CacheOptions?.Invoke(cacheOptions);
|
||||||
|
|
||||||
@ -48,6 +49,24 @@ public static class ServiceCollectionExtensions
|
|||||||
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
|
return new JwtTokenManagerService(options, httpClientFactory, logger, memoryCache, cacheOptions);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!_defaultRegistered)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IJwtTokenManagerService>(sp =>
|
||||||
|
sp.GetRequiredKeyedService<IJwtTokenManagerService>(name));
|
||||||
|
|
||||||
|
_defaultRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
return services;
|
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