Working with strongly typed configuration in .Net Core

Since .net core appeared we have change the way we work with settings.

Nowadays  web.config no longer exists so we have to work with json files.

There are many ways to work with this configuration. However, in this sample we are going to focus only in strongly typed configuration.
If you want to read more about the different options we have to work with confoguration please follow this link

On Startup we read configurations file, in our case appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "TupeSettings": {
    "ConnectionString": "***************************"
  },
  "CommonSettings": {
    "Application": "test application",
    "Mail": "guillermo.fernandez@cavepot.com"
  }
}

Usually you have one configuration file for the environment. In that case you have to read the appropriate configuration file on the  startup, depending on the environment.
If you want to go deep working with multiple environments please follow this link.

On ConfigurationServices we will configure our settings to service and bound to our classes (TupeSettings and CommonSettings) This classes must have public properties and cannot be abstract classes.

public class CommonSettings
{
   public string Application { get; set; }
   public string Mail { get; set; }
}
public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions();
        services.Configure<TupeSettings>(Configuration.GetSection("TupeSettings"));
        services.Configure<CommonSettings>(Configuration.GetSection("CommonSettings"));
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseMvc();
    }
}

Here is the big question? How and where can I use my settings.
All configuration is managed using IOptions, so the only thing you have to do is to inyect the settings you need.
In our  sample we inyect TupeSettings and CommonSettings.

public class SettingsController : Controller
{
   private readonly TupeSettings _settings;
   private readonly CommonSettings _common;

   public SettingsController(IOptions options, IOptions common) {
      this._settings = options.Value;
      this._common = common.Value;
   }

   [Route("api/settings/get")]
   [HttpGet]
   public object Get()
   {
      return new {TupeSettings = this._settings, CommonSettings = this._common};
   }
}

IOptionsSnapshot can reload configuration data when it changes. If you want to go deep please follow this link.
If you see the image bellow you can see that in CommonSetting we have the setting that we have in our json file.
contructor

I hope this sample helps you!

If you want to download  the code please go to github. We will keep working on this and more examples of .net core on this repository.

Thanks!!!

Related posts

Comments are closed.