Browse Source

Update the DbMigrator project of the sample app

pull/171/head
gdlcf88 4 years ago
parent
commit
6ba7d4dbf0
  1. 62
      samples/EShopSample/aspnet-core/src/EShopSample.DbMigrator/DbMigratorHostedService.cs
  2. 69
      samples/EShopSample/aspnet-core/src/EShopSample.DbMigrator/EShopSample.DbMigrator.csproj
  3. 49
      samples/EShopSample/aspnet-core/src/EShopSample.DbMigrator/Program.cs

62
samples/EShopSample/aspnet-core/src/EShopSample.DbMigrator/DbMigratorHostedService.cs

@ -1,43 +1,49 @@
using System.Threading;
using System.Threading.Tasks;
using EShopSample.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using EShopSample.Data;
using Serilog;
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)
{
using (var application = await AbpApplicationFactory.CreateAsync<EShopSampleDbMigratorModule>(options =>
{
options.UseAutofac();
options.Services.AddLogging(c => c.AddSerilog());
}))
{
await application.InitializeAsync();
await application
.ServiceProvider
.GetRequiredService<EShopSampleDbMigrationService>()
.MigrateAsync();
await application.ShutdownAsync();
_hostApplicationLifetime.StopApplication();
}
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;
}
}
}

69
samples/EShopSample/aspnet-core/src/EShopSample.DbMigrator/EShopSample.DbMigrator.csproj

@ -1,34 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.*" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" />
<ProjectReference Include="..\EShopSample.Application.Contracts\EShopSample.Application.Contracts.csproj" />
<ProjectReference Include="..\EShopSample.EntityFrameworkCore\EShopSample.EntityFrameworkCore.csproj" />
</ItemGroup>
<Import Project="..\..\common.props"/>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json"/>
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json"/>
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0"/>
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0"/>
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
<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"/>
<ProjectReference Include="..\EShopSample.EntityFrameworkCore\EShopSample.EntityFrameworkCore.csproj"/>
</ItemGroup>
<ItemGroup>
<Compile Remove="Logs\**"/>
<Content Remove="Logs\**"/>
<EmbeddedResource Remove="Logs\**"/>
<None Remove="Logs\**"/>
</ItemGroup>
</Project>

49
samples/EShopSample/aspnet-core/src/EShopSample.DbMigrator/Program.cs

@ -1,38 +1,39 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
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()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning)
#if DEBUG
.MinimumLevel.Override("EShopSample", LogEventLevel.Debug)
.MinimumLevel.Override("EShopSample", LogEventLevel.Debug)
#else
.MinimumLevel.Override("EShopSample", LogEventLevel.Information)
#endif
.Enrich.FromLogContext()
.WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "Logs/logs.txt"))
.WriteTo.Console()
.CreateLogger();
await CreateHostBuilder(args).RunConsoleAsync();
}
.Enrich.FromLogContext()
.WriteTo.Async(c => c.File("Logs/logs.txt"))
.WriteTo.Async(c => c.Console())
.CreateLogger();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<DbMigratorHostedService>();
});
await CreateHostBuilder(args).RunConsoleAsync();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.AddAppSettingsSecretsJson()
.ConfigureLogging((context, logging) => logging.ClearProviders())
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<DbMigratorHostedService>();
});
}
Loading…
Cancel
Save