user13146150
user13146150

Reputation:

how to get env variable in controller or other classes

I have this custom ConfigurationService, everything was working fine, but I need to load settings based on env now. So to do that I have this method LoadAppSettings

public class ConfigurationService : IConfigurationService
    {

        public IConfigurationRoot LoadAppSettings(IHostingEnvironment env)
        {
            try
            {
                var config = new ConfigurationBuilder()
                .SetBasePath(_basePath)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) //for azure functions
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

                return config;
            }
            catch (System.IO.FileNotFoundException)
            {
                return null;
            }
        }
    }

When calling this method from other places, like controller, how can I access current env?

IConfigurationRoot Config
    {
        get
        {
            if (_config == null)
            {
                _config = ConfigurationService.LoadAppSettings(ENV ?);
            }
            return _config;
        }
    }

Upvotes: 1

Views: 9658

Answers (1)

Farhad Zamani
Farhad Zamani

Reputation: 5861

You must inject IHostingEnvironment interface in contractor controller like this.

public class HomeController : ControllerBase
{
    private readonly IHostingEnvironment _hostingEnvironment;
    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }
}

then pass the IHostingEnvironment to method

public IActionResult Index()
{
    //test
    _config = ConfigurationService.LoadAppSettings(_hostingEnvironment);
    return Ok();
}

Update

Another way is using IOptions<>

in this case you must register an class that showing environment variable like this

public class AppSettings
{
    public string Environment { get; set; }
}

register class in ConfigureServices method and getEnvironment from CurrentEnvironment.EnvironmentName

public IConfiguration Configuration { get; }
private Microsoft.AspNetCore.Hosting.IHostingEnvironment CurrentEnvironment { get; set; }
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
    Configuration = configuration;
    CurrentEnvironment = env;
}
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(a => new AppSettings { Environment = CurrentEnvironment.EnvironmentName });
}

then in your controller inject IOption<AppSettings> instead of IHostingEnvironment and change LoadAppSettings(IHostingEnvironment env) to LoadAppSettings(AppSettings appSettings)

Controller

public class HomeController : ControllerBase
{
    private readonly AppSettings _appSettings;
    public HomeController(IOptions<AppSettings> options)
    {
        _appSettings = options.Value;
    }
}

Action

public IActionResult Index()
{
    //test
    _config = ConfigurationService.LoadAppSettings(_appSettings);
    return Ok();
}

and finally change the LoadAppSettings method to this

public IConfigurationRoot LoadAppSettings(AppSettings env)
{
    try
    {
        var config = new ConfigurationBuilder()
                .SetBasePath(_basePath)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) //for azure functions
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

        return config;
    }
    catch (System.IO.FileNotFoundException)
    {
         return null;
    }

Upvotes: 2

Related Questions