From decd0faafd81a72b5e5157379d3912f26309c1da Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Sun, 24 Sep 2023 09:50:10 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20refactoring=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Application/DependencyInjection.cs | 4 ++-- src/Application/Services/Picklist/PicklistService.cs | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Application/DependencyInjection.cs b/src/Application/DependencyInjection.cs index 469289d24..d1c72a17d 100644 --- a/src/Application/DependencyInjection.cs +++ b/src/Application/DependencyInjection.cs @@ -35,8 +35,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection options.UseReduxDevTools(); }); services.AddLazyCache(); - services.AddScoped(); - services.AddScoped(sp => { + services.AddSingleton(); + services.AddSingleton(sp => { var service = sp.GetRequiredService(); service.Initialize(); return service; diff --git a/src/Application/Services/Picklist/PicklistService.cs b/src/Application/Services/Picklist/PicklistService.cs index c436a47f4..ae213ce69 100644 --- a/src/Application/Services/Picklist/PicklistService.cs +++ b/src/Application/Services/Picklist/PicklistService.cs @@ -1,5 +1,6 @@ using CleanArchitecture.Blazor.Application.Features.KeyValues.Caching; using CleanArchitecture.Blazor.Application.Features.KeyValues.DTOs; +using Microsoft.Extensions.DependencyInjection; namespace CleanArchitecture.Blazor.Application.Services.Picklist; @@ -11,10 +12,12 @@ public class PicklistService : IPicklistService public PicklistService( IAppCache cache, - IApplicationDbContext context, IMapper mapper) + IServiceScopeFactory scopeFactory, + IMapper mapper) { _cache = cache; - _context = context; + var scope = scopeFactory.CreateScope(); + _context = scope.ServiceProvider.GetRequiredService(); _mapper = mapper; }