diff --git a/IdentityServer.Application/DependencyInjectionExtensions.cs b/IdentityServer.Application/DependencyInjectionExtensions.cs index a987a02..548e741 100644 --- a/IdentityServer.Application/DependencyInjectionExtensions.cs +++ b/IdentityServer.Application/DependencyInjectionExtensions.cs @@ -14,7 +14,7 @@ namespace IdentityServer.Application services.AddSingleton(); services.AddSingleton(); services.AddScoped(); - services.AddScoped(); + services.AddSingleton(); } private static void AddStores(this IServiceCollection services) diff --git a/IdentityServer.Application/Services/BehaviorService.cs b/IdentityServer.Application/Services/BehaviorService.cs index 48257a6..bbd33c9 100644 --- a/IdentityServer.Application/Services/BehaviorService.cs +++ b/IdentityServer.Application/Services/BehaviorService.cs @@ -1,20 +1,26 @@ using IdentityServer.Application.Services.Abstractions; using IdentityServer.Application.Stores; +using IdentityServer.Domain.Entities; using IdentityServer.Domain.Models; using IdentityServer.Domain.Repositories; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System; using System.Threading.Tasks; namespace IdentityServer.Application.Services { internal class BehaviorService : IBehaviorService { + private readonly IServiceProvider _serviceProvider; + private readonly ILogger _logger; private readonly ITokenStore _securityStore; - private readonly IIdentityRepository _identityRepository; - public BehaviorService(ITokenStore securityStore, IIdentityRepository identityRepository) + public BehaviorService(IServiceProvider serviceProvider, ILogger logger, ITokenStore securityStore) { + _serviceProvider = serviceProvider; + _logger = logger; _securityStore = securityStore; - _identityRepository = identityRepository; } public void FillTokenStore() @@ -22,10 +28,17 @@ namespace IdentityServer.Application.Services public async Task FillTokenStoreAsync() { - var activeTokens = await _identityRepository.GetActiveTokens(); + var activeTokens = Array.Empty(); + using (var scope = _serviceProvider.CreateScope()) + { + var _repository = scope.ServiceProvider.GetRequiredService(); + activeTokens = await _repository.GetActiveTokens(); + } + if (activeTokens.Length <= 0) return; + _logger.LogInformation($"BehaviorService: {activeTokens.Length} active tokens were found in database."); foreach (var token in activeTokens) { var storeToken = new Token() { Raw = token.Token, ValidFrom = token.ValidFrom, ValidUntil = token.ValidUntil };