Skip to content

Instantly share code, notes, and snippets.

@fredrikhr
Last active June 7, 2019 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikhr/3761673e2e26006699a0a1e3c3705970 to your computer and use it in GitHub Desktop.
Save fredrikhr/3761673e2e26006699a0a1e3c3705970 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<UserSecretsId>0864c15e-1de4-426a-97df-a43cb2700536</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
</ItemGroup>
</Project>
using System;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Couven92.SeanAlford.ConsoleApp
{
public static class Program
{
public static async Task Main(string[] args)
{
var host = new HostBuilder()
.ConfigureHostConfiguration(config =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
config.AddUserSecrets(typeof(Program).Assembly, optional: true);
config.AddCommandLine(args);
})
.ConfigureLogging((hostContext, logging) =>
{
logging.AddConfiguration(hostContext.Configuration.GetSection(nameof(Microsoft.Extensions.Logging)));
logging.AddConsole();
})
.ConfigureServices((hostContext, services) =>
{
services.AddDbContext<SqlServerContext>(db =>
{
var config = hostContext.Configuration;
string connectionString = config.GetConnectionString("db");
db.UseSqlServer(connectionString);
});
services.AddSingleton<ISystemNode, SystemNode>();
services.AddSingleton<Terminal>();
})
.UseConsoleLifetime().Build();
using (host)
{
await host.StartAsync();
var serviceProvider = host.Services;
var terminal = serviceProvider.GetRequiredService<Terminal>();
terminal.Run(args);
await host.StopAsync();
}
}
}
internal class Terminal
{
public Terminal(ILogger<Terminal> logger, SqlServerContext dbContext, IApplicationLifetime lifetime)
{
Logger = logger;
DbContext = dbContext;
Lifetime = lifetime;
}
public ILogger<Terminal> Logger { get; }
public SqlServerContext DbContext { get; }
public IApplicationLifetime Lifetime { get; }
internal void Run(string[] args)
{
Logger.LogCritical(new NotImplementedException(), null);
}
}
internal interface ISystemNode
{
}
internal class SystemNode : ISystemNode
{
}
internal class SqlServerContext : DbContext
{
public SqlServerContext(DbContextOptions<SqlServerContext> options)
: base(options) { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment