Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IOptions in ConfigureServices method?

I have asp.net core application. I want to use IOptions pattern to inject values from appsettings.json. So I have a class SecurityHeaderOptions, and also have target class SecurityHeadersBuilder whose constructor takes IOptions<SecurityHeaderOptions> as parameter.

I know that .net core can implicitly create instance of SecurityHeadersBuilder by injecting IOptions<SecurityHeaderOptions> after registering both with container.

However i want to explicitly create instance of SecurityHeadersBuilder, call one of its method and then register the instance with the container.

public sealed class SecurityHeaderOptions {     public string FrameOption { get; set; }         public string XssProtection { get; set; } }   public class SecurityHeadersBuilder {     private readonly SecurityHeaderOptions _options = null;      public SecurityHeadersBuilder(IOptions<SecurityHeaderOptions> options)     {         _options = options.Value;         }      public SecurityHeadersBuilder AddDefaultPolicy()     {         AddFrameOptions();         AddConetntSecurityPolicy();         return this;     } } 

ConfigureServices method

public void ConfigureServices(IServiceCollection services) {             services.Configure<SecurityHeaderOptions>(Configuration.GetSection("SecurityHeaderOptions"));      services.AddScoped<SecurityHeadersBuilder>(provider =>             new SecurityHeadersBuilder(?????).AddDefaultPolicy()) } 

Questions
1> If i am explicitly passing options into constructor, do i need to register SecurityHeaderOptions with the container using service.Configure method?

2> Configuration.GetSection("SecurityHeaderOptions") can't return instance of IOptions<SecurityHeaderOptions> , instead it returns IConfigurationSection?

3>Either way, how do I retrieve and pass SecurityHeaderOptions into SecurityHeadersBuilder's constructor?

like image 808
LP13 Avatar asked Mar 21 '17 16:03

LP13


People also ask

What is IOptions?

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?

The ConfigureServices method is a place where you can register your dependent classes with the built-in IoC container. After registering dependent class, it can be used anywhere in the application. You just need to include it in the parameter of the constructor of a class where you want to use it.

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)


Video Answer


2 Answers

Using .NET Core 2 and not having a provider available (or caring to add it) in ConfigureServices I opted to go with something like this (using OP code as example):

public void ConfigureServices(IServiceCollection services) {     // secOpts available for use in ConfigureServices     var secOpts = Configuration         .GetSection("SecurityHeaderOptions")         .Get<SecurityHeaderOptions>();      ... } 
like image 162
Quinton Smith Avatar answered Sep 30 '22 19:09

Quinton Smith


This is how I register options and inject into SecurityHeadersBuilder

public void ConfigureServices(IServiceCollection services) {     services.Configure<SecurityHeaderOptions>(Configuration.GetSection("SecurityHeaderOptions"));                 services.AddScoped<SecurityHeadersBuilder>(provider =>     {         var option = provider.GetService<IOptions<SecurityHeaderOptions>>();         return new SecurityHeadersBuilder(option)             .AddDefaultPolicy();     }); } 
like image 30
LP13 Avatar answered Sep 30 '22 18:09

LP13