.NET Core console application, how to configure appSettings per environment?

后端 未结 7 980
囚心锁ツ
囚心锁ツ 2020-12-04 14:15

I have a .NET Core 1.0.0 console application and two environments. I need to be able to use appSettings.dev.json and appSettings.test.json based on

7条回答
  •  半阙折子戏
    2020-12-04 14:31

    It's something like this, for a dotnet 2.x core console application:

            using Microsoft.Extensions.Configuration;
            using Microsoft.Extensions.DependencyInjection;
            using Microsoft.Extensions.Logging;
    
            [...]
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();
            var serviceProvider = new ServiceCollection()
                .AddLogging(options => options.AddConfiguration(configuration).AddConsole())
                .AddSingleton(configuration)
                .AddSingleton()
                .BuildServiceProvider();
            [...]
            await serviceProvider.GetService().Start();
    

    The you could inject ILoggerFactory, IConfiguration in the SomeService.

提交回复
热议问题