Startup.cs in a self-hosted .NET Core Console Application

后端 未结 7 934
情书的邮戳
情书的邮戳 2020-12-07 09:53

I have a self-hosted .NET Core Console Application.

The web shows examples for ASP.NET Core but i do not have a webserver. Just a simple c

7条回答
  •  被撕碎了的回忆
    2020-12-07 10:22

    Another way would be using HostBuilder from Microsoft.Extensions.Hosting package.

    public static async Task Main(string[] args)
    {
        var builder = new HostBuilder()
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());
                config.AddJsonFile("appsettings.json", true);
                if (args != null) config.AddCommandLine(args);
            })
            .ConfigureServices((hostingContext, services) =>
            {
                services.AddHostedService();
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration);
                logging.AddConsole();
            });
    
        await builder.RunConsoleAsync();
    }
    

提交回复
热议问题