I only found a use for ioptions-it's convenient to use strongly-typed configurations in. NET core applications.
If there is no ioptions, using a strongly-typed configuration requires that you resolve the following 2 issues yourself:
1) Deserialize the configuration in the configuration file (such as Appsettings.json) into an instance of the configuration class
2) Single example of injected configuration class
If you have ioptions, you can either pass configure<toptions> (iconfiguration config) or configure<toptions> (Action <TOptions> configureoptions) extension method to fix the above 2 questions at once.
Let's look at an example where we're going to be Ali greater than. NET client topsdk when migrating to. NET core, you want to implement configuring the client through Appsettings.json.
{" topclient": { "ServerURL": "", "AppKey": "", "Appsecret": " " }}
A configuration class topclientoptions is defined for this purpose:
Public class topclientoptions{ publicstringgetset;} Public string Get Set ; } Public string Get Set ; }}
Then, by adding the following code to the startup Configureservices (), you can bind the configuration in Appsettings.json to the instance of Topclientoptions and complete the dependency injection.
Services. AddOptions ();
Services. Configure<topclientoptions> (Configuration.getsection ("topclient"));
(Note: Need to install NuGet package Microsoft.Extensions.Options.ConfigurationExtensions)
Only the constructor can be injected where you need to read the configuration in topclientoptions:
Public class defaulttopclient:itopclient{ public defaulttopclient ( ioptions<topclientoptions> Options) { }}
What is ioptions in. NET Core