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

后端 未结 7 951
囚心锁ツ
囚心锁ツ 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:25

    There are two IHostingEnvironment interfaces that you should use. One is for ASP.NET Core Applications, the other one is for .NET Core Console applications. You can use this code example for both:

    using System;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.FileProviders;
    using Microsoft.Extensions.Hosting.Internal;
    
    namespace MyApplication.Common
    {
        public static class ConfigurationFactory
        {
            /// <summary>
            /// Use for ASP.NET Core Web applications.
            /// </summary>
            /// <param name="config"></param>
            /// <param name="env"></param>
            /// <returns></returns>
            public static IConfigurationBuilder Configure(IConfigurationBuilder config, IHostingEnvironment env)
            {
                return Configure(config, env.EnvironmentName);
            }
    
            /// <summary>
            /// Use for .NET Core Console applications.
            /// </summary>
            /// <param name="config"></param>
            /// <param name="env"></param>
            /// <returns></returns>
            private static IConfigurationBuilder Configure(IConfigurationBuilder config, Microsoft.Extensions.Hosting.IHostingEnvironment env)
            {
                return Configure(config, env.EnvironmentName);
            }
    
            private static IConfigurationBuilder Configure(IConfigurationBuilder config, string environmentName)
            {
                return config
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables();
            }
    
            /// <summary>
            /// Use for .NET Core Console applications.
            /// </summary>
            /// <returns></returns>
            public static IConfiguration CreateConfiguration()
            {
                var env = new HostingEnvironment
                {
                    EnvironmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"),
                    ApplicationName = AppDomain.CurrentDomain.FriendlyName,
                    ContentRootPath = AppDomain.CurrentDomain.BaseDirectory,
                    ContentRootFileProvider = new PhysicalFileProvider(AppDomain.CurrentDomain.BaseDirectory)
                };
    
                var config = new ConfigurationBuilder();
                var configured = Configure(config, env);
                return configured.Build();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:28

    These environment things seems to work for most ppl, but I don't aggree at all with all that environmental management. Whether the runtime nor the target system knows what it is. Only you, as a developer or a deployment mechanism, knows what the target system is.

    Everyone is talking about ASPNETCORE_ENVIRONMENT variable, even official documentation like here https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.0. But in fact someone has to define a system explicitly as production system for example, by setting ASPNETCORE_ENVIRONMENT manually once. Do you really want to assume and rely on it that this is already set in every environment you use? No, you can't. What if you have to deploy a console app to a batch server where no website is running? ASPNETCORE_ENVIRONMENT is not available. What if you need to deploy and run a .net core webapi without IIS, only with kestrel? No web.config and no environment variable. Do you want your admins/operation team to set this misleading variable for your console app? In this context i've seen lots of projects which have appsettings like this per project:

    appsettings.json
    appsettings.development.json
    appsettings.test.json
    appsettings.uat.json
    appsettings.staging.json
    appsettings.production.json
    

    Keep in mind that by default each of these files will be published and also be deployed to the target system. At a first look it looks very easy to let the environment "decide" which config should be used. But you have configuration and also potentially credentials deployed on a system which is not intended for it.

    Conclusion

    I recommend appsettings.json + appsettings.release.json. First one is only for dev. Change it, play with it like you want. Last one is a VALID config ready for deployment (process). Before deployment starts, transform the config to be ready for the target system. That's it. No need to rely on settings on the target machine, no messy configs. Keep full control of your app even when servers change quickly (like scaling in general, VMs, cloud, whatever)

    I appreciate constructive feedback :-)

    0 讨论(0)
  • 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<IConfiguration>(configuration)
                .AddSingleton<SomeService>()
                .BuildServiceProvider();
            [...]
            await serviceProvider.GetService<SomeService>().Start();
    

    The you could inject ILoggerFactory, IConfiguration in the SomeService.

    0 讨论(0)
  • 2020-12-04 14:36

    If like me, you're simply trying to have a different configuration file for Release and Development mode, just add a appsettings.Development.json file with CopyToOutputDirectory setting set to true in the file's property window.

    Now, to access the file depending on the build configuration, you can use the #if DEBUG preprocessor directive.

    Here's an example :

    static void Main(string[] args)
    {
    
    #if DEBUG
        var builder = new ConfigurationBuilder()
                .AddJsonFile($"appsettings.Development.json", true, true);
    #else
        var builder = new ConfigurationBuilder()
                .AddJsonFile($"appsettings.json", true, true);
    #endif
    
        var configuration = builder.Build();
    
        // ... use configuration
    }
    
    0 讨论(0)
  • 2020-12-04 14:39

    You can do this for ASP.Net Core environment variable (ASPNETCORE_ENVIRONMENT): -

    using Microsoft.AspNetCore.Hosting;
    using System;
    
    public class Program {
        private static string HostingEnvironment => Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        private static bool IsEnvironment(string environmentName) => HostingEnvironment?.ToLower() == environmentName?.ToLower() && null != environmentName;
    
        private static bool Development => IsEnvironment(EnvironmentName.Development);
        private static bool Production => IsEnvironment(EnvironmentName.Production);
        private static bool Staging => IsEnvironment(EnvironmentName.Staging);
    
        public static void Main(string[] args) { // Your code here }
    }
    

    Then you can simply use the property

        public static void Main(string[] args) {
            if (Development){
                // Blow up the planet
            }
        }
    
    0 讨论(0)
  • 2020-12-04 14:42

    This is how we do it in our .netcore console app. The key here is to include the right dependencies on your project namely (may be not all, check based on your needs) and copy to output the appSetting.json as part of your buildoptions

      {
        "buildOptions": {
        "emitEntryPoint": true,
        "copyToOutput": {
           "include": [
           "appsettings*.json",
           "App*.config"
                     ]
              }
    },
    
      using Microsoft.Extensions.Configuration;
      namespace MyApp
      {
        public static void Main(string[] args)
        {
            var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    
    
            var builder = new ConfigurationBuilder()
                .AddJsonFile($"appsettings.json", true, true)
                .AddJsonFile($"appsettings.{environmentName}.json", true, true)
                .AddEnvironmentVariables();
            var configuration = builder.Build();
            var myConnString= configuration.GetConnectionString("SQLConn");
        }
    

    }

    0 讨论(0)
提交回复
热议问题