简体   繁体   English

如何在启动 class 中使用强类型配置

[英]How can I use a strongly-typed config within the Startup class

I have a strongly-typed config working but I am struggling with using the strongly-typed class within the "class Startup"我有一个强类型配置工作,但我正在努力在“类启动”中使用强类型 class

Here is my code in startup:这是我在启动时的代码:

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IHostEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Allow sites to hit the API
        services.AddCors();

        // Add Mvc Routes
        services.AddMvc(option => option.EnableEndpointRouting = false);

        // Add custom configuration
        services.Configure<Config>(Configuration);

I'd like to use the strongly-typed class in another function in that file, but I'm not sure how I can do that...我想在该文件的另一个 function 中使用强类型 class ,但我不确定我该怎么做......

This is how it works in other classes:这是它在其他类中的工作方式:

    readonly Config Config = null;

    public EmailController(IOptions<Config> config) 
    {
        Config = config.Value;
    }

But if I try to add the same code to my "class Startup" then I get this exception但是,如果我尝试将相同的代码添加到我的“类启动”中,那么我会得到这个异常

System.ArgumentNullException: 'Value cannot be null. System.ArgumentNullException: '值不能是 null。 (Parameter 'config')' (参数'配置')'

on the line在线上

services.Configure<Config>(Configuration);

I think you are asking how can you use your config inside the startup class before you are allowed to inject it into a constructor?我想您是在问如何在启动 class 中使用您的配置,然后才能将其注入构造函数?

You can use Get or Bind methods on IConfiguration您可以在 IConfiguration 上使用 Get 或 Bind 方法


// Add custom configuration
services.Configure<Config>(Configuration);

//Get the config object for use here
var config = Configuration.Get<Config>();


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将聚合物库与强类型视图一起使用? - How can i use Polymer library with Strongly-Typed View? 如何在服务器标签中使用强类型资源? - How can I use strongly-typed resources in server tags? 如何使用强型卫星组件? - How to use strongly-typed satellite assembly? Objective-C转换为C#:如果字典是强类型的,如何在C#中使用多个类型的.plist? - Objective-C converting to C#: How can I use a .plist with multiple types in C# if Dictionary is strongly-typed? 我可以使用什么强类型数据结构来保存具有不同形状的多个RecordSet的集合 - What Strongly-Typed Data-Structure can I use to hold a collection of multiple RecordSets with different shape 如何使用强类型字典 <string,Type> 将字符串与类关联 - How to use strongly-typed like dictionary <string,Type> to associate a string to a class 我可以使用什么数据结构来表示.Net中的强类型2D数据矩阵? - What data structures can I use to represent a strongly-typed 2D matrix of data in .Net? 使用ServiceStack.Redis,如何在事务中运行强类型的只读查询 - Using ServiceStack.Redis, how can I run strongly-typed read-only queries in a transaction 强类型模型:如何处理下拉列表? - Strongly-Typed Model: How do I handle dropdown lists? 类的强类型搜索功能? - Strongly-typed Search functionality for class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM