From 1530b694c57520082eb8f30224564f32cf31ecb3 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Mon, 15 Apr 2024 22:52:58 +0800 Subject: [PATCH] Fix InitSerive not working issue --- src/AgileConfig.Server.Apisite/InitService.cs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/AgileConfig.Server.Apisite/InitService.cs b/src/AgileConfig.Server.Apisite/InitService.cs index e37913af..a44dbb3d 100644 --- a/src/AgileConfig.Server.Apisite/InitService.cs +++ b/src/AgileConfig.Server.Apisite/InitService.cs @@ -15,24 +15,21 @@ public class InitService : IHostedService { private readonly IRemoteServerNodeProxy _remoteServerNodeProxy; private readonly IEventRegister _eventRegister; - private readonly ISettingService _settingService; private readonly IServerNodeService _serverNodeService; private readonly IServiceHealthCheckService _serviceHealthCheckService; private readonly ISystemInitializationService _systemInitializationService; private readonly ILogger _logger; + private readonly IServiceScope _localServiceScope; public InitService(IServiceScopeFactory serviceScopeFactory, ISystemInitializationService systemInitializationService, ILogger logger) { _logger = logger; _systemInitializationService = systemInitializationService; - using (var scope = serviceScopeFactory.CreateScope()) - { - _remoteServerNodeProxy = scope.ServiceProvider.GetService(); - _eventRegister = scope.ServiceProvider.GetService(); - _settingService = scope.ServiceProvider.GetService(); - _serverNodeService = scope.ServiceProvider.GetService(); - _serverNodeService = scope.ServiceProvider.GetService(); - _serviceHealthCheckService = scope.ServiceProvider.GetService(); - } + _localServiceScope = serviceScopeFactory.CreateScope(); + _remoteServerNodeProxy = _localServiceScope.ServiceProvider.GetService(); + _eventRegister = _localServiceScope.ServiceProvider.GetService(); + _serverNodeService = _localServiceScope.ServiceProvider.GetService(); + _serverNodeService = _localServiceScope.ServiceProvider.GetService(); + _serviceHealthCheckService = _localServiceScope.ServiceProvider.GetService(); } public async Task StartAsync(CancellationToken cancellationToken) @@ -53,7 +50,7 @@ public async Task StartAsync(CancellationToken cancellationToken) if (!string.IsNullOrEmpty(ip)) { var desc = Appsettings.IsAdminConsoleMode ? "控制台节点" : ""; - _ =_serverNodeService.JoinAsync(ip, 5000, desc); + _ = _serverNodeService.JoinAsync(ip, 5000, desc); _logger.LogInformation($"AgileConfig node http://{ip}:5000 joined ."); } } @@ -70,6 +67,8 @@ public async Task StopAsync(CancellationToken cancellationToken) _logger.LogInformation($"AgileConfig node http://{ip}:5000 removed ."); } } + + _localServiceScope?.Dispose(); } private string GetIp()