Actually read AppSettings in ConfigureServices phase in ASP.NET Core

asp.net core read appsettings.json from class library
asp.net core 2.0 configuration
asp.net core get configuration in static method
how to read values from appsettings.json in c#
asp.net core 2.0 strongly typed configuration
asp net core 2.2 get configuration in controller
configuration.getsection returns null
.net core get appsetting variable

I need to setup a few dependencies (services) in the ConfigureServices method in an ASP.NET Core 1.0 web application.

The issue is that based on the new JSON configuration I need to setup a service or another.

I can't seem to actually read the settings in the ConfigureServices phase of the app lifetime:

public void ConfigureServices(IServiceCollection services)
{
    var section = Configuration.GetSection("MySettings"); // this does not actually hold the settings
    services.Configure<MySettingsClass>(section); // this is a setup instruction, I can't actually get a MySettingsClass instance with the settings
    // ...
    // set up services
    services.AddSingleton(typeof(ISomething), typeof(ConcreteSomething));
}

I would need to actually read that section and decide what to register for ISomething (maybe a different type than ConcreteSomething).

That is the way you can get the typed settings from appSettings.json right in ConfigureServices method:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MySettings>(Configuration.GetSection(nameof(MySettings)));
        services.AddSingleton(Configuration);

        // ...

        var settings = Configuration.GetSection(nameof(MySettings)).Get<MySettings>();
        int maxNumberOfSomething = settings.MaxNumberOfSomething;

        // ...
    }

    // ...
}

How to read appsettings.json values ASP.NET Core, I need to setup a few dependencies (services) in the ConfigureServices method in an ASP.NET Core 1.0 web application. The issue is that  In this article, we will understand, how can we read the AppSettings from appsettings.json file in ASP.NET Core 3.1. Step 1: Create a new ASP.NET Core project in Visual Studio 2019. Step 2: On this screen, you can provide your project name, solution name and it's location.

Starting from ASP.NET Core 2.0 we do configuration setup in Program class when building WebHost instance. Example of such setup:

return new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .ConfigureAppConfiguration((builderContext, config) =>
    {
        IHostingEnvironment env = builderContext.HostingEnvironment;

        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
    })

Among others, this allows using configuration directly in Startup class, getting an instance of IConfiguration via constructor injection (thank you, built-in DI container):

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    ...
}

Accessing Configuration Settings in ASP.NET Core -- Visual Studio , I need to setup a few dependencies (services) in the ConfigureServices method in an ASP.NET Core 1.0 web application. The issue is that based on the new  Today, I was rewriting an old ASP.NET MVC5 Demo project to ASP.NET Core, and found that the way we used to read Web.config by ConfigurationManager.AppSettings [] is no longer working. . NET Core has many new ways to achieve this. I picked one that suitable for my project. Here is how I do it. This is how we used to read web.config in ASP.NET.

You can access appsettings.json values by Configuration["ConfigSection:ConfigValue"])

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<MyContext>(o => 
            o.UseSqlServer(Configuration["AppSettings:SqlConn"]));
    }
}

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "System": "Information",
      "Microsoft": "Warning"
    }
  },
  "AppSettings": {
    "SqlConn": "Data Source=MyServer\\MyInstance;Initial Catalog=MyDb;User ID=sa;Password=password;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;"
  }
}

Taking advantage of configuration in .net core 2.2+ console , Actually read AppSettings in ConfigureServices phase in ASP.NET Core. I need to setup a few dependencies (services) in the ConfigureServices method in an  In today’s article, we will see how to read values from appsettings.json in ASP.NET Core similarly to what we used to do in ASP.NET MVC by reading values from web.config. Join Public Speaking Virtual Conference

Read AppSettings in ASP.NET Core 2.x, In ASP.NET Core, your web.config file with its <appsettings> section is gone. In addition, my CutOffDate setting (when I get around to retrieving it) of this in the ConfigureServices method of my application's Startup class. App configuration in ASP.NET Core is based on key-value pairs established by configuration providers. Configuration providers read configuration data into key-value pairs from a variety of configuration sources: Configuration packages for common configuration provider scenarios ( Microsoft.Extensions.Configuration) are included implicitly by

How to Build and Secure Web APIs with ASP.NET Core 3.0, This is related my prior article on dependency injection in .net core 2.2 console apps. I am assuming you've read that before coming here. private static IServiceCollection ConfigureServices() This assumes our application uses a file named appsettings.json to hold our config settings (I REALLY hope  Actually read AppSettings in ConfigureServices phase in ASP.NET Core. I need to setup a few dependencies (services) in the ConfigureServices method in an ASP.NET Core 1.0 web application. The issue is that based on the new JSON configuration I need to setup a service or another.

Migrating ASP.NET Microservices to ASP.NET Core: By Example, NET Core, and found that the way we used to read Web.config by ConfigurationManager. NET Core settings file is called appsettings.json, instead of web.config. public void ConfigureServices(IServiceCollection services) { services. Actually web.config has had a dedicated section for a while - why  With ASP.NET Core 2.0 (basically Core 1.1+), the IConfiguration is injected to Startup, and that can be used within ConfigureServices() and Configure() methods. As shown in the accepted answer, the configuration can be bound to an object. But if just one value is required, the key based approach works well.

Comments
  • See stackoverflow.com/q/40397648/5426333
  • @ademcaglin : Thanks! That was it. I voted for closing my own question as a duplicate of that one :)
  • The linked answer is to get values from the config file, not the appsettings.json file.
  • Possible duplicate of ASP.NET Core Configuration Section in Startup
  • yeah! worked for me. finally i can get my stuff from json entries to prepare my services :\
  • How does this work when an ASP.NET Core 2.1 application is hosted under IIS? The WebHostBuilder won't be used, so what is in that IConfiguration configuration that's passed into Startup's constructor?