Browse Source

Update the DbMigrator project of the sample app

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

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

@ -1,26 +1,30 @@
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 IHostApplicationLifetime _hostApplicationLifetime;
private readonly IConfiguration _configuration;
public DbMigratorHostedService(IHostApplicationLifetime hostApplicationLifetime) public DbMigratorHostedService(IHostApplicationLifetime hostApplicationLifetime, IConfiguration configuration)
{ {
_hostApplicationLifetime = hostApplicationLifetime; _hostApplicationLifetime = hostApplicationLifetime;
_configuration = configuration;
} }
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
using (var application = await AbpApplicationFactory.CreateAsync<EShopSampleDbMigratorModule>(options => using (var application = await AbpApplicationFactory.CreateAsync<EShopSampleDbMigratorModule>(options =>
{ {
options.Services.ReplaceConfiguration(_configuration);
options.UseAutofac(); options.UseAutofac();
options.Services.AddLogging(c => c.AddSerilog()); options.Services.AddLogging(c => c.AddSerilog());
})) }))
@ -38,6 +42,8 @@ namespace EShopSample.DbMigrator
} }
} }
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
} }
} }

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

@ -1,6 +1,6 @@
<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>
@ -8,10 +8,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="appsettings.json" /> <None Remove="appsettings.json"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Remove="appsettings.json"/>
<Content Include="appsettings.json"> <Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
@ -19,16 +20,24 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" /> <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0"/>
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.Async" Version="1.5.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.*" /> <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)" /> <PackageReference Include="Volo.Abp.Autofac" Version="$(AbpVersion)"/>
<ProjectReference Include="..\EShopSample.Application.Contracts\EShopSample.Application.Contracts.csproj" /> <ProjectReference Include="..\EShopSample.Application.Contracts\EShopSample.Application.Contracts.csproj"/>
<ProjectReference Include="..\EShopSample.EntityFrameworkCore\EShopSample.EntityFrameworkCore.csproj" /> <ProjectReference Include="..\EShopSample.EntityFrameworkCore\EShopSample.EntityFrameworkCore.csproj"/>
</ItemGroup>
<ItemGroup>
<Compile Remove="Logs\**"/>
<Content Remove="Logs\**"/>
<EmbeddedResource Remove="Logs\**"/>
<None Remove="Logs\**"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

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

@ -1,14 +1,14 @@
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() Log.Logger = new LoggerConfiguration()
@ -21,8 +21,8 @@ namespace EShopSample.DbMigrator
.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(); await CreateHostBuilder(args).RunConsoleAsync();
@ -30,9 +30,10 @@ namespace EShopSample.DbMigrator
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.AddAppSettingsSecretsJson()
.ConfigureLogging((context, logging) => logging.ClearProviders())
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>
{ {
services.AddHostedService<DbMigratorHostedService>(); services.AddHostedService<DbMigratorHostedService>();
}); });
}
} }
Loading…
Cancel
Save