How exactly does Microsoft.Extensions.Configuration dependent on ASP.NET Core?

后端 未结 1 1400
悲&欢浪女
悲&欢浪女 2021-02-01 19:26

Does ASP.NET Core implement IConfiguration access to config values?

Most likely my question arose because I don\'t understand what exactly ASP.NET Core is.

1条回答
  •  执笔经年
    2021-02-01 19:38

    Microsoft.Extensions.Configuration, like other packages in the Microsoft.Extensions namespace (e.g. Options, or DependencyInjection), are packages that were created as part of the ASP.NET Core framework. The way ASP.NET Core and all its related packages were built however is in a very modular way, so all the libraries can be used within the ASP.NET Core context, or without.

    You have to understand those packages just as libraries. They are included in ASP.NET Core since the framework builds on top of them, but if you do not need the ASP.NET Core web framework, you can still use those libraries separately without any mention of ASP.NET Core. That’s actually why they live inside the Microsoft.Extensions namespace instead of Microsoft.AspNetCore: They are completely separate projects. Of course, development of those packages is done by the ASP.NET Core team and the design decisions of ASP.NET Core do affect how those extension packages evolve; but the team is very careful with these packages so that the general use is not affected.

    So that all being said, how do you use these packages? Just like any other library, you just add a NuGet reference to it. Since Microsoft.Extensions.Configuration is the base library which does not come with any facility to load files, you also need Microsoft.Extensions.Configuration.Json if you want to load JSON files.

    But then it’s really straight forward:

    var configuration = new ConfigurationBuilder()
        .AddJsonFile("config.json")
        .Build();
    
    // retrieve configuration values
    Console.WriteLine(configuration["foo"]); // bar
    Console.WriteLine(configuration["baz:bar"]); // qux
    

    For this example, the config.json looked like this:

    {
        "foo": "bar",
        "baz": {
            "bar": "qux"
        }
    }
    

    So you can just load the configuration like this. Be sure to still check the documentation though. It may be about the configuration used inside of ASP.NET Core but the underlying concepts still apply (e.g. how configuration paths look like, or how binding works).

    Finally, note that this is really just meant for configuration. Loading data from JSON is just one of many configuration sources you can with Microsoft.Extensions.Configuration. But regardless of what provider you will use, you will end up with the same configuration format that has the concepts of sections and key paths.

    If you came to the package while looking how to parse JSON, then it’s likely that you are looking at the wrong tool. If you want to parse JSON to retrieve a proper data structure, like you would use when using JSON as a way to serialize data, then you should look at something different. The most common solution for parsing JSON (serializing too) is using Json.NET which is a very powerful and flexible tool to deal with any kind of JSON data.

    0 讨论(0)
提交回复
热议问题