.net core 在Program类里如何使用配置文件?

在Program 中设置通过配置文件设置监听端口

在Program中配置:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .UseContentRoot(AppContext.BaseDirectory)
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>().UseKestrel((hostingContext, options) =>
                {
                    //opt.ListenLocalhost(5088); 本地监听
                    //options.ListenAnyIP(5089);
                    options.Configure(hostingContext.Configuration.GetSection("Kestrel"));
                });
            }).ConfigureLogging((context, logging) =>
            {
                logging.ClearProviders();
                logging.AddConsole();
                logging.SetMinimumLevel(LogLevel.Trace);
            }).UseNLog();

在appsetting.json中配置

"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://*:30080"
    }
  }
}
原文地址:https://www.cnblogs.com/TbKing-blogs/p/15493665.html