Skip to content

Instantly share code, notes, and snippets.

@panicoenlaxbox
Last active August 4, 2023 04:52
Show Gist options
  • Save panicoenlaxbox/2332d1c1e38b6c7ba89a54395963d059 to your computer and use it in GitHub Desktop.
Save panicoenlaxbox/2332d1c1e38b6c7ba89a54395963d059 to your computer and use it in GitHub Desktop.
.NET Core Generic host with EF Core
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Debug"
}
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=(LocalDB)\\MSSQLLocalDB;Database=Shop;Trusted_Connection=True;"
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SavingData
{
public class ShopContext : DbContext
{
public ShopContext(DbContextOptions options) : base(options)
{
}
public DbSet<Order> Orders { get; set; }
}
public class Order
{
public int Id { get; set; }
public DateTime OrderDate { get; set; }
}
class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ShopContext>
{
public ShopContext CreateDbContext(string[] args)
{
var options = new DbContextOptionsBuilder<ShopContext>()
.UseSqlServer(@"Server=(LocalDB)\MSSQLLocalDB;Database=Shop;Trusted_Connection=True;")
.Options;
return new ShopContext(options);
}
}
class Program
{
private static async Task Main(string[] args)
{
var host = new HostBuilder()
.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) =>
{
configurationBuilder.AddJsonFile("appsettings.json", optional: true);
configurationBuilder.AddJsonFile($"appsettings.{hostBuilderContext.HostingEnvironment.EnvironmentName}.json", optional: true);
configurationBuilder.AddEnvironmentVariables();
})
.ConfigureLogging((hostBuilderContext, builder) =>
{
var config = hostBuilderContext.Configuration.GetSection("Logging");
builder.AddConfiguration(config);
builder.AddConsole();
})
.ConfigureServices((hostBuilderContext, serviceCollection) =>
{
serviceCollection.AddDbContext<ShopContext>(options =>
{
options
.UseSqlServer(hostBuilderContext.Configuration.GetConnectionString("DefaultConnection"));
});
serviceCollection.AddScoped<IHostedService, MyHostedService>();
});
await host.RunConsoleAsync();
}
}
public class MyHostedService : IHostedService
{
private readonly ILogger<MyHostedService> _logger;
private readonly IApplicationLifetime _applicationLifetime;
private readonly IConfiguration _configuration;
private readonly ShopContext _context;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _task;
public MyHostedService(ILogger<MyHostedService> logger, IApplicationLifetime applicationLifetime, IConfiguration configuration, ShopContext context)
{
_logger = logger;
_applicationLifetime = applicationLifetime;
_configuration = configuration;
_context = context;
}
private async Task ExecuteAsync()
{
_logger.LogDebug(nameof(ExecuteAsync));
try
{
await _context.Database.MigrateAsync(_cancellationTokenSource.Token);
//while (!_cancellationTokenSource.IsCancellationRequested)
//{
//}
}
catch (Exception e)
{
_logger.LogError(e.ToString());
Environment.Exit(-1);
}
finally
{
_logger.LogDebug($"{nameof(ExecuteAsync)}.finally");
}
if (!_cancellationTokenSource.IsCancellationRequested)
{
_logger.LogDebug(nameof(_applicationLifetime.StopApplication));
_applicationLifetime.StopApplication();
}
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogDebug(nameof(StartAsync));
_task = Task.Run(ExecuteAsync);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogDebug(nameof(StopAsync));
if (_task.IsCompleted)
{
return Task.CompletedTask;
}
_cancellationTokenSource.Cancel();
return Task.WhenAny(_task, Task.Delay(Timeout.Infinite, cancellationToken));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment