mirror of https://github.com/EasyAbp/EShop.git
3 changed files with 98 additions and 82 deletions
@ -1,43 +1,49 @@ |
|||||
using System.Threading; |
using System.Threading; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
|
using EShopSample.Data; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
using Microsoft.Extensions.Hosting; |
using Microsoft.Extensions.Hosting; |
||||
using EShopSample.Data; |
|
||||
using Serilog; |
using Serilog; |
||||
using Volo.Abp; |
using Volo.Abp; |
||||
|
|
||||
namespace EShopSample.DbMigrator |
namespace EShopSample.DbMigrator; |
||||
|
|
||||
|
public class DbMigratorHostedService : IHostedService |
||||
{ |
{ |
||||
public class DbMigratorHostedService : IHostedService |
private readonly IHostApplicationLifetime _hostApplicationLifetime; |
||||
|
private readonly IConfiguration _configuration; |
||||
|
|
||||
|
public DbMigratorHostedService(IHostApplicationLifetime hostApplicationLifetime, IConfiguration configuration) |
||||
{ |
{ |
||||
private readonly IHostApplicationLifetime _hostApplicationLifetime; |
_hostApplicationLifetime = hostApplicationLifetime; |
||||
|
_configuration = configuration; |
||||
|
} |
||||
|
|
||||
public DbMigratorHostedService(IHostApplicationLifetime hostApplicationLifetime) |
public async Task StartAsync(CancellationToken cancellationToken) |
||||
|
{ |
||||
|
using (var application = await AbpApplicationFactory.CreateAsync<EShopSampleDbMigratorModule>(options => |
||||
|
{ |
||||
|
options.Services.ReplaceConfiguration(_configuration); |
||||
|
options.UseAutofac(); |
||||
|
options.Services.AddLogging(c => c.AddSerilog()); |
||||
|
})) |
||||
{ |
{ |
||||
_hostApplicationLifetime = hostApplicationLifetime; |
await application.InitializeAsync(); |
||||
} |
|
||||
|
|
||||
public async Task StartAsync(CancellationToken cancellationToken) |
await application |
||||
{ |
.ServiceProvider |
||||
using (var application = await AbpApplicationFactory.CreateAsync<EShopSampleDbMigratorModule>(options => |
.GetRequiredService<EShopSampleDbMigrationService>() |
||||
{ |
.MigrateAsync(); |
||||
options.UseAutofac(); |
|
||||
options.Services.AddLogging(c => c.AddSerilog()); |
await application.ShutdownAsync(); |
||||
})) |
|
||||
{ |
_hostApplicationLifetime.StopApplication(); |
||||
await application.InitializeAsync(); |
|
||||
|
|
||||
await application |
|
||||
.ServiceProvider |
|
||||
.GetRequiredService<EShopSampleDbMigrationService>() |
|
||||
.MigrateAsync(); |
|
||||
|
|
||||
await application.ShutdownAsync(); |
|
||||
|
|
||||
_hostApplicationLifetime.StopApplication(); |
|
||||
} |
|
||||
} |
} |
||||
|
} |
||||
|
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
public Task StopAsync(CancellationToken cancellationToken) |
||||
|
{ |
||||
|
return Task.CompletedTask; |
||||
} |
} |
||||
} |
} |
||||
@ -1,34 +1,43 @@ |
|||||
<Project Sdk="Microsoft.NET.Sdk"> |
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
<Import Project="..\..\common.props" /> |
<Import Project="..\..\common.props"/> |
||||
|
|
||||
<PropertyGroup> |
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
<OutputType>Exe</OutputType> |
||||
<TargetFramework>net6.0</TargetFramework> |
<TargetFramework>net6.0</TargetFramework> |
||||
</PropertyGroup> |
</PropertyGroup> |
||||
|
|
||||
<ItemGroup> |
<ItemGroup> |
||||
<None Remove="appsettings.json" /> |
<None Remove="appsettings.json"/> |
||||
</ItemGroup> |
</ItemGroup> |
||||
|
|
||||
<ItemGroup> |
<ItemGroup> |
||||
<Content Include="appsettings.json"> |
<None Remove="appsettings.json"/> |
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> |
<Content Include="appsettings.json"> |
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> |
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> |
||||
</Content> |
<CopyToOutputDirectory>Always</CopyToOutputDirectory> |
||||
</ItemGroup> |
</Content> |
||||
|
</ItemGroup> |
||||
<ItemGroup> |
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" /> |
<ItemGroup> |
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> |
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0"/> |
||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" /> |
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0"/> |
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.*" /> |
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/> |
||||
</ItemGroup> |
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1"/> |
||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1"/> |
||||
<ItemGroup> |
</ItemGroup> |
||||
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" /> |
|
||||
<ProjectReference Include="..\EShopSample.Application.Contracts\EShopSample.Application.Contracts.csproj" /> |
<ItemGroup> |
||||
<ProjectReference Include="..\EShopSample.EntityFrameworkCore\EShopSample.EntityFrameworkCore.csproj" /> |
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)"/> |
||||
</ItemGroup> |
<ProjectReference Include="..\EShopSample.Application.Contracts\EShopSample.Application.Contracts.csproj"/> |
||||
|
<ProjectReference Include="..\EShopSample.EntityFrameworkCore\EShopSample.EntityFrameworkCore.csproj"/> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<Compile Remove="Logs\**"/> |
||||
|
<Content Remove="Logs\**"/> |
||||
|
<EmbeddedResource Remove="Logs\**"/> |
||||
|
<None Remove="Logs\**"/> |
||||
|
</ItemGroup> |
||||
|
|
||||
</Project> |
</Project> |
||||
|
|||||
@ -1,38 +1,39 @@ |
|||||
using System.IO; |
|
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
using Microsoft.Extensions.Hosting; |
using Microsoft.Extensions.Hosting; |
||||
|
using Microsoft.Extensions.Logging; |
||||
using Serilog; |
using Serilog; |
||||
using Serilog.Events; |
using Serilog.Events; |
||||
|
|
||||
namespace EShopSample.DbMigrator |
namespace EShopSample.DbMigrator; |
||||
|
|
||||
|
class Program |
||||
{ |
{ |
||||
class Program |
static async Task Main(string[] args) |
||||
{ |
{ |
||||
static async Task Main(string[] args) |
Log.Logger = new LoggerConfiguration() |
||||
{ |
.MinimumLevel.Information() |
||||
Log.Logger = new LoggerConfiguration() |
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) |
||||
.MinimumLevel.Information() |
.MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning) |
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) |
|
||||
.MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning) |
|
||||
#if DEBUG
|
#if DEBUG
|
||||
.MinimumLevel.Override("EShopSample", LogEventLevel.Debug) |
.MinimumLevel.Override("EShopSample", LogEventLevel.Debug) |
||||
#else
|
#else
|
||||
.MinimumLevel.Override("EShopSample", LogEventLevel.Information) |
.MinimumLevel.Override("EShopSample", LogEventLevel.Information) |
||||
#endif
|
#endif
|
||||
.Enrich.FromLogContext() |
.Enrich.FromLogContext() |
||||
.WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt")) |
.WriteTo.Async(c => c.File("Logs/logs.txt")) |
||||
.WriteTo.Console() |
.WriteTo.Async(c => c.Console()) |
||||
.CreateLogger(); |
.CreateLogger(); |
||||
|
|
||||
await CreateHostBuilder(args).RunConsoleAsync(); |
|
||||
} |
|
||||
|
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) => |
await CreateHostBuilder(args).RunConsoleAsync(); |
||||
Host.CreateDefaultBuilder(args) |
|
||||
.ConfigureServices((hostContext, services) => |
|
||||
{ |
|
||||
services.AddHostedService<DbMigratorHostedService>(); |
|
||||
}); |
|
||||
} |
} |
||||
} |
|
||||
|
public static IHostBuilder CreateHostBuilder(string[] args) => |
||||
|
Host.CreateDefaultBuilder(args) |
||||
|
.AddAppSettingsSecretsJson() |
||||
|
.ConfigureLogging((context, logging) => logging.ClearProviders()) |
||||
|
.ConfigureServices((hostContext, services) => |
||||
|
{ |
||||
|
services.AddHostedService<DbMigratorHostedService>(); |
||||
|
}); |
||||
|
} |
||||
Loading…
Reference in new issue