asp.net-core


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?
Regarding your questions:
1. Yes, you need to register the options, but I believe you are doing it the wrong way (at least by your example). You should register as this:
services.Configure<SecurityHeaderOptions>(options => Configuration.GetSection("SecurityHeaderOptions").Bind(options));
2. I believe that the correct registration I refer above returns what you are expecting.
3. Just registering it and placing it on the SecurityHeaderBuilder constructor is enough. You do not need (neither does the default .NET Core IOC container allows) to pass constructor parameters when registering it. For that you would need to use other IOC's such as Autofac.
But you need to register SecurityHeadersBuilder in order to use it within other classes.
Just use an interface for that.
public interface ISecurityHeadersBuilder
{
SecurityHeadersBuilder AddDefaultPolicy;
}
public class SecurityHeadersBuilder : ISecurityHeadersBuilder
{
private readonly SecurityHeaderOptions _options = null;
public SecurityHeadersBuilder(IOptions<SecurityHeaderOptions> options)
{
_options = options.Value;
}
public SecurityHeadersBuilder AddDefaultPolicy()
{
AddFrameOptions();
AddConetntSecurityPolicy();
return this;
}
}
Then, just register it in startup.cs as this
services.AddScoped<ISecurityHeadersBuilder, SecurityHeadersBuilder>();
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();
});
}

Related Links

How to order an asp-src-include statement?
HtmlHelper to render nested Tag Helpers in MVC 6 vNext
Configure DI using Json config
ASP.NET Core (vNext) project can't find reference to 4.51
How To Deploy MVC 6 Application On IIS without publish
RemoteIpAddress is always null
Asp.Net Core MVC RC-1 validation doesn't show up when javascript is disabled
ASP.NET Core Application Initialization on Azure
Kestrel error when sending a request from an emulator
asp.net 5 (core 1) logging confusion - wanting to only see “debug” messages
How to set my user info globally once authenticated
ASP.NET 5 (MVC 6) - Resources Localization
IPAddress null for MVC 6 ASP.NET 5
asp.net 5 (core 1) - getting authentication data in asp.net mvc web api seems different
Named Routes with anchor tag helper
ASP.NET 5 (MVC6) How to seed users

Categories

HOME
twitter
osgi
oracle11g
ngrx
angular-material
react-virtualized
google-oauth
webpack-2
sql-server-2016
directx
upload
pheatmap
primary-key
modx-revolution
iolanguage
solaris-10
openedx
firefox-webextensions
scichart
intentfilter
crystal-reports-2010
shopware
pepper
facebook-access-token
brunch
phpfox
linkerd
windows-server-2000
subdomains
delicious-api
bpel
tinymce-4
preg-match
galsim
twilio-api
code-contracts
perlin-noise
mpmediaquery
bower-install
revolution-slider
retina-display
multilingual
temporary-files
elasticsearch-plugin
fakeiteasy
outlook-api
avconv
pubmed
winscp-net
integrity
mu
brightcove
nativeapplication
windows-mobile-6.5
akka-cluster
color-profile
dwscript
deadbolt-2
merge-conflict-resolution
blackberry-10
python-stackless
php-internals
tcpserver
cartesian-product
icu4j
marching-cubes
jmeter-maven-plugin
deis
tableau-online
graph-drawing
operation
sonarqube5.1.2
dia
jsonpickle
relocation
fpml
terminfo
tld
valuechangelistener
algebraic-data-types
flashvars
delphi-6
gdata-api
objective-c-2.0
buildr
invite
jmock
radcombobox
webkit.net
virtual-functions

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App