¶Ô±ÈÐÂÎļþ |
| | |
| | | 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<SeedDataHostedService> _logger; |
| | | private readonly string _webRootPath; |
| | | private readonly IServiceProvider _serviceProvider; |
| | | |
| | | public SeedDataHostedService( |
| | | IServiceProvider serviceProvider, |
| | | IWebHostEnvironment webHostEnvironment, |
| | | ILogger<SeedDataHostedService> logger) |
| | | { |
| | | _serviceProvider = serviceProvider; |
| | | _logger = logger; |
| | | _webRootPath = webHostEnvironment.WebRootPath; |
| | | |
| | | using var scope = _serviceProvider.CreateScope(); |
| | | |
| | | var dbContext = scope.ServiceProvider.GetService<DBContext>(); |
| | | //dbContext.Db.Aop.DataExecuting = SqlSugarAop.DataExecuting; |
| | | _dbContext = dbContext; |
| | | } |
| | | |
| | | public Task StartAsync(CancellationToken cancellationToken) |
| | | { |
| | | _logger.LogInformation("Start Initialization Db Seed Service!"); |
| | | DoWork(); |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | | private void DoWork() |
| | | { |
| | | try |
| | | { |
| | | //if (AppSettings.app("AppSettings", "SeedDBEnabled").ObjToBool() || AppSettings.app("AppSettings", "SeedDBDataEnabled").ObjToBool()) |
| | | { |
| | | // ä½¿ç¨ myScopedService æ§è¡ä»»å¡ |
| | | |
| | | 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; |
| | | } |
| | | |
| | | } |
| | | } |