Skip to content

Instantly share code, notes, and snippets.

@yzorg
Last active June 11, 2021 16:31

Revisions

  1. yzorg revised this gist Jun 11, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Program.cs
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
    return Host.CreateDefaultBuilder()
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
    config.AddKeyPerFile(directoryPath: "/app/secrets/project", optional: true);
    config.AddKeyPerFile(directoryPath: "/app/secrets/shared", optional: true);
    config.AddKeyPerFile(directoryPath: "/app/secrets/namespace", optional: true);

    config.AddCommandLine(args, switchMappings);
  2. yzorg created this gist Jun 11, 2021.
    66 changes: 66 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    using System;
    using System.Collections.Generic;

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;

    namespace Middle.MyApp
    {
    class Program
    {
    static void Main(string[] args)
    {
    CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
    var switchMappings = new Dictionary<string, string>()
    {
    { "-s", "Start" },
    { "--start", "Start" }
    };

    return Host.CreateDefaultBuilder()
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
    config.AddKeyPerFile(directoryPath: "/app/secrets/project", optional: true);
    config.AddKeyPerFile(directoryPath: "/app/secrets/namespace", optional: true);

    config.AddCommandLine(args, switchMappings);
    })
    //.ConfigureLogging(opt => { opt.AddConsole(c => { c.TimestampFormat = "[HH:mm:ss] "; }); }) // CORE31
    .ConfigureLogging(opt =>
    {
    opt.AddSimpleConsole(consoleOpt =>
    {
    consoleOpt.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.ff ";
    //consoleOpt.UseUtcTimestamp = true; // not needed, k8s node local time will be UTC, and I'd prefer not to see UTC when running on Win10/WSL
    });
    })
    .ConfigureServices((hostContext, services) =>
    {
    new Startup(hostContext.Configuration)
    .ConfigureServices(services);
    });
    }

    public record Startup(IConfiguration Configuration)
    {
    public void ConfigureServices(IServiceCollection services)
    {
    services.Configure<LoaderOptions>(Configuration);
    services.AddDbContextFactory<Models.AppSqlContext>(options =>
    {
    options.UseSqlServer(Configuration.GetConnectionString("AppSql"), sqlOptions =>
    {
    sqlOptions.EnableRetryOnFailure(3);
    });
    });

    services.AddHostedService<Worker>();
    }
    }
    }