Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between services.Configure() and services.AddOptions<T>().Bind() when loading configuration in ASP.NET Core?

In my ASP.NET Core 2.2 WebApi project, I want to load configuration from appsettings.json into strongly typed object.

The appsettings.json has following configuration section:

{   "MySettings1": {     "Name": "John Smith",     "Age": "25",   } } 

which I want to load into strongly typed object MySettings:

public class MySettings {     public string Name { get; set; }     public int Age { get; set; } } 

I can do this in my Startup.ConfigureServices() method either like this:

services.Configure<MySettings>(configuration.GetSection("MySettings1")); 

or like this:

services.AddOptions<MySettings>().Bind(configuration.GetSection("MySettings1")); 

What is the difference between these two approaches? Both of them work fine as I am able to get proper instance of IOptions<MySettings> injected in HomeController in both cases.

Are there some specific scenarios in which I should use one over the other approach? (for example, in the future I will probably want to add some kind of runtime validation of the MySettings object once it's populated from the configuration, so in this case should I prefer one approach over the other?)

like image 882
mlst Avatar asked Apr 19 '19 13:04

mlst


People also ask

What is difference between configure and ConfigureServices?

They are the ConfigureServices method and the Configure method. In the Startup class, we actually do two things: Configure Service(): It is used to add services to the container and configure those services. basically, service is a component that is intended for common consumption in the application.

What is services AddOptions?

AddOptions(IServiceCollection) Adds services required for using options. AddOptions<TOptions>(IServiceCollection) Gets an options builder that forwards Configure calls for the same named TOptions to the underlying service collection. AddOptions<TOptions>(IServiceCollection, String)

What is Ioption in ASP.NET Core?

IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies. IOptionsSnapshot is a Scoped service and provides a snapshot of the options at the time the IOptionsSnapshot<T> object is constructed.

How do I configure services in NET Core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings. json.


1 Answers

This was asked in a Github issue in November 2018 Question: AddOptions() vs. Multiple Configure(…). Both methods do the same job but AddOptions came later and allows more customizations.

Configure(Action configureOptions) and OptionsBuilder.Configure(Action configureOptions) will both end up doing the same thing:

services.AddSingleton<IConfigureOptions<TOptions>>(     new ConfigureNamedOptions<TOptions>(name, configureOptions)); 

And OptionsBuilder.Bind(IConfiguration config) will actually call Configure(IConfiguration config) directly, so they are also equivalent.

So both APIs are interchangeable. And you can expect the services.Configure calls to continue to work. The options builder API came later to allow for a bit more control with various utility methods. But it’s not a replacement for the direct services.Configure API.

like image 156
Panagiotis Kanavos Avatar answered Sep 23 '22 14:09

Panagiotis Kanavos