using HslCommunication.WebSocket; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core.Helper; using WIDESEA_Core.Seed; namespace WIDESEA_Core { public sealed class SeedDataHostedService : IHostedService { private readonly DBContext _dbContext; private readonly ILogger _logger; private readonly string _webRootPath; public SeedDataHostedService(IServiceProvider serviceProvider, IWebHostEnvironment webHostEnvironment, ILogger logger, DBContext dbContext) { _logger = logger; _webRootPath = webHostEnvironment.WebRootPath; _dbContext = dbContext; } public Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("Start Initialization Db Seed Service!"); DoWork(); return Task.CompletedTask; } private void DoWork() { try { if (AppSettings.GetValue("DBSeedEnable").ObjToBool()) { DBSeed.SeedAsync(_dbContext, _webRootPath); //多租户 同步 //await DBSeed.TenantSeedAsync(_dbContext); } } catch (Exception ex) { _logger.LogError(ex, "Error occured seeding the Database."); throw; } } public Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("Stop Initialization Db Seed Service!"); return Task.CompletedTask; } } }