Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to Configuration object from Startup class

I would like to access to the Active Directory from my company in many controllers from my ASP.NET vNext project, and I inserted the domain name into my config.json file, so I can access it from the Configuration class. I find it heavy to always instantiate a new Configuration object at every time I want to access to my config.json, is there any way through the IConfiguration API to access to the Configuration class initialized into the Startup class ?

like image 575
Christophe Gigax Avatar asked Mar 06 '15 11:03

Christophe Gigax


People also ask

What's the Configure method on the startup class used for?

The Configure method is used to specify how the app responds to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but it isn't registered in the service container.

How do I access configuration in any class in ASP.NET Core?

In an ASP.NET core application, the "appsettings. json" file is where we can keep configurations and read them using JsonConfigurationProvider provided by Microsoft. In this article, we will see how to keep configuration values in appsettings. json file and how to read those values and use them in an application.

How do I get IConfiguration in .NET Core?

Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.

What does configure () and ConfigureServices () methods do startup CS do?

The startup class contains two methods: ConfigureServices(): Registers the services that your application will need. Configure(): Configures the middleware pipeline that controls how the application processes the HTTP requests and sends the response.


1 Answers

An example of how you can do this:

Let's assume you have a config.json like below:

{
    "SomeSetting1": "some value here",
    "SomeSetting2": "some value here",
    "SomeSetting3": "some value here",
    "ActiveDirectory": {
        "DomainName": "DOMAIN-NAME-HERE"
    }
}

Create a POCO type having your option information:

public class ActiveDirectoryOptions
{
    public string DomainName { get; set; }
}

In Startup.cs, when configuring services:

services.Configure<ActiveDirectoryOptions>(optionsSetup =>
{
    //get from config.json file
    optionsSetup.DomainName = configuration.Get("ActiveDirectory:DomainName");
});

In all controllers which want to get this config setting, do something like...Here the options is injected by the DI system:

public class HomeController : Controller
{
    private readonly IOptions<ActiveDirectoryOptions> _activeDirectoryOptions;

    public HomeController(IOptions<ActiveDirectoryOptions> activeDirectoryOptions)
    {
        _activeDirectoryOptions = activeDirectoryOptions;
    }

    public IActionResult Index()
    {
        string domainName = _activeDirectoryOptions.Options.DomainName;

        ........
    }
}

Responding to the comment:
There are couple of options that I can think of:

  1. From within the action, you can do

    var options = HttpContext.RequestServices.GetRequiredService<IOptions<ActiveDirectoryOptions>>().Options;

  2. You can have a parameter to the action which is decorated with FromServicesAttribute. This attribute will cause the parameter value to be retrieved from the DI. Example:

    public IActionResult Index([FromServices] IOptions<ActiveDirectoryOptions> options)

I prefer #2 over #1 as in case of unit testing it gives you information on all dependent pieces.

like image 184
Kiran Avatar answered Nov 14 '22 22:11

Kiran