本文介绍了如何在 ConfigureServices 方法中获取 IOptions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 asp.net 核心应用程序.我想使用 IOptions 模式从 appsettings.json 注入值.所以我有一个类 SecurityHeaderOptions,还有目标类 SecurityHeadersBuilder,它的构造函数以 IOptions 作为参数.

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.

我知道 .net core 可以通过在容器注册后注入 IOptions 隐式创建 SecurityHeadersBuilder 的实例.

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

但是我想显式创建 SecurityHeadersBuilder 的实例,调用其方法之一,然后向容器注册该实例.

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 方法

public void ConfigureServices(IServiceCollection services)
{        
    services.Configure<SecurityHeaderOptions>(Configuration.GetSection("SecurityHeaderOptions"));

    services.AddScoped<SecurityHeadersBuilder>(provider => 
           new SecurityHeadersBuilder(?????).AddDefaultPolicy())
}

问题
1> 如果我明确地将选项传递给构造函数,我是否需要使用 service.Configure 方法向容器注册 SecurityHeaderOptions ?

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") 无法返回 IOptions 的实例,而是返回 IConfigurationSection?

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

3>无论哪种方式,我如何检索并将 SecurityHeaderOptions 传递到 SecurityHeadersBuilder 的构造函数中?

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

推荐答案

使用 .NET Core 2 并且在 ConfigureServices 中没有可用的提供程序(或不想添加它)我选择了一些东西像这样(以 OP 代码为例):

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>();

    ...
}

这篇关于如何在 ConfigureServices 方法中获取 IOptions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:14