-3

I have added the access to the appsettings.json file as a framework service in my Startup.cs:

public IConfigurationRoot Configuration { get; }

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.Configure<AppConfig>(Configuration);
    services.AddMvc();
}

So now I have access to the configuration file from my controllers:

   public class HomeController : Controller
    {
        private readonly AppConfig _appConfig;

        public HomeController(IOptions<AppConfig> appConfig, ConfigContext configContext)
        {
            _appConfig = appConfig.Value;
        }
}

That's working but what's currently a good practice in netcoreapps for accessing the config file from classes outsite my controller scope?

I mean that I would not like to pass always the required config variables to other methods, example:

public IActionResult AnyAction() {
  SomeStaticClass.SomeMethod(_appConfig.var1, _appConfig.var2, _appConfig.var3...)

 //or always have to pass the _appConfig reference

  SomeStaticClass.SomeMethod(_appConfig)
}

In previous versions of .NET Framework if I required access to the config file from "SomeStaticClass" I used to use ConfigurationManager in any class that I need access to the web.config.

What's the correct way to do it in a netcoreapp1.1 ? either ConfigurationManager like or dependency injection approach works for me.

ferflores
  • 964
  • 2
  • 17
  • 34
  • 1
    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration#using-options-and-configuration-objects – Alexan Apr 10 '17 at 20:59
  • Ok I guess I should create a class that exposes the config file as the service for the framework so I can use it anywhere – ferflores Apr 10 '17 at 21:01

2 Answers2

0

I think this question is more about how you can get a contextual variable from a static class. I think this will accomplish what you want but I'm not sure why you want a static class or what you are trying to do with it (see XY Problem).

   public class HomeController : Controller
    {
        private readonly AppConfig _appConfig;

        public HomeController(IOptions<AppConfig> appConfig, ConfigContext configContext)
        {
            _appConfig = appConfig.Value;
            SomeStaticClass.SomeStaticMember = appConfig.Value
        }
        public IActionResult AnyAction() {
            SomeStaticClass.SomeMethod(); //The get the value you want from within
        }
    }

EDIT: You can use Newtonsoft.Json, it's a dependency of Microsoft.AspNet.Mvc.ModelBinding which is a dependency of Microsoft.AspNet.Mvc

string fileContents = string.Empty;
using (StreamReader file = File.OpenText(@".\appsettings.json"))
{
    fileContents = file.ReadAllLines();
}
configs= JsonConvert.DeserializeObject(fileContents );
Paul Totzke
  • 1,220
  • 15
  • 30
0

What I did is to create the following class:

public static class Configuration
{
    private static IConfiguration _configuration;

    static Configuration()
    {
        var builder = new ConfigurationBuilder()
         .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        _configuration = builder.Build();
    }

    public static string GetValue(string key)
    {
        return _configuration.GetValue<string>(key, null);
    }

    public static string GetValue(string section, string key)
    {
        return _configuration.GetSection(section).GetValue<string>(key);
    }
}

However it doesn't use the environment logic that is used in Startup.cs using the IHostingEnvironment parameter.

ferflores
  • 964
  • 2
  • 17
  • 34
  • 2
    Please read about how dependency injection works. You can inject `IOptions` (almost) everywhere – Tseng Apr 10 '17 at 22:49