Microsoft.Extensions.Configuration 究竟如何依赖于 ASP.NET Core? [英] How exactly does Microsoft.Extensions.Configuration dependent on ASP.NET Core?

查看:43
本文介绍了Microsoft.Extensions.Configuration 究竟如何依赖于 ASP.NET Core?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET Core 是否实现了IConfiguration 对配置值的访问?

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

我的问题很可能是因为我不明白 ASP.NET Core 到底是什么.嗯,我知道它是一个 web 框架,不确定,但看起来它是 .NET 中的命名空间,或者一个包......我知道在 php 中,一个框架可以是一组类(命名空间)或编译库是作为扩展提供的,所以我假设在 .NET 中有类似的方法.

Most likely my question arose because I don't understand what exactly ASP.NET Core is. Well, I know it's a web framework, Not sure, but looks like it is a namespace in .NET, or a package... I know in php, a framework could be a set of classes (a namespace) or compiled library which is provided as an extension so I presume a similar approach in .NET.

最初,我并不打算将头放在 ASP.NET Core 上.我需要为我的简单控制台 C# 应用程序(VS Code 和 .NET Core)存储一些配置.我发现了很多主题(例如这里:如何从控制台应用程序中的 config.json 读取值) 以读取 JSON(推荐)配置.鉴于此,我添加了三个必要的 nugget 包:

Initially, I didn't intend to wrap my head around ASP.NET Core yet. I needed to store some config for my simple console C# application (VS Code and .NET Core). I've found a lot of topics (for example here: How to read values from config.json in Console Application) that to read JSON (recommended) config. Given that, I added three necessary nugget packages:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration.Json;

我需要使用:

new ConfigurationBuilder()          
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json").Build();

这将返回一个实现 IConfigurationRoot/IConfiguration 接口的对象.但是所有示例都是在 ASP.NET Core 上下文中给出的.我有一个非常简单的应用程序,我还不需要任何 ASP.NET 功能.

This returns an object that implements the IConfigurationRoot/IConfiguration interface. But all the examples are given in an ASP.NET Core context. I have a really simple app and I don't need any of ASP.NET functionality yet.

所以我尝试在没有 ASP.NET 的情况下访问 IConfigurationRoot.生成的对象存储来自配置文件的值,但没有其接口的所有方法来访问它们.

So I've tried to access IConfigurationRoot without ASP.NET. The resulting object stores values from config file, but does not have all methods of its interface to access them.

如何在 .NET 命名空间的上下文中解释这一点?ASP.NET Core 是否实现了从 IConfiguration 访问值的方法,例如 Get()?

How to explain this in context of .NET namespaces? Does ASP.NET Core implement methods to access values from IConfiguration like Get<T>()?

如果 Microsoft.Extensions.ConfigurationMicrosoft.AspNetCore.App 的一部分或严重依赖于它,为什么它在不同的命名空间中?

If Microsoft.Extensions.Configuration is part of or heavily dependent on Microsoft.AspNetCore.App, why is it in different namespace?

如果我添加 ASP.NET Core(NuGet 包和命名空间),会不会有点矫枉过正?

If I add ASP.NET Core (NuGet package and namespaces), will it be an overkill?

也许我应该使用 ConfigurationBuilder 以外的其他东西来读取 JSON?

Maybe I should use soemthing other than ConfigurationBuilder to read JSON?

推荐答案

Microsoft.Extensions.Configuration,就像 Microsoft.Extensions 命名空间中的其他包一样(例如 选项,或DependencyInjection),是作为 ASP.NET Core 框架的一部分创建的包.然而,ASP.NET Core 及其所有相关包的构建方式是非常模块化的,因此所有库都可以在 ASP.NET Core 上下文中使用,也可以不使用.

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.

您必须将这些包理解为库.它们包含在 ASP.NET Core 中,因为框架构建在它们之上,但是如果您不需要 ASP.NET Core Web 框架,您仍然可以单独使用这些库,而无需提及 ASP.NET 核心.这就是为什么它们位于 Microsoft.Extensions 命名空间而不是 Microsoft.AspNetCore 中的原因:它们是完全独立的项目.当然,这些包的开发是由 ASP.NET Core 团队完成的,而且 ASP.NET Core 的设计决策确实会影响这些扩展包的发展方式;但是团队对这些包非常小心,不影响一般使用.

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.

综上所述,您如何使用这些软件包?就像任何其他库一样,您只需添加一个 NuGet 引用即可.由于Microsoft.Extensions.Configuration基础 库,它不附带任何加载文件的工具,因此您还需要Microsoft.Extensions.Configuration.Json 如果你想加载 JSON 文件.

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.

但它真的很简单:

var configuration = new ConfigurationBuilder()
    .AddJsonFile("config.json")
    .Build();

// retrieve configuration values
Console.WriteLine(configuration["foo"]); // bar
Console.WriteLine(configuration["baz:bar"]); // qux

对于这个例子,config.json 看起来像这样:

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

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

所以你可以像这样加载配置.请务必仍查看文档 不过.它可能与 ASP.NET Core 内部使用的配置有关,但底层概念仍然适用(例如 配置路径的样子,或绑定的工作原理).

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).

最后,请注意,这实际上只是用于配置.从 JSON 加载数据只是您可以使用 Microsoft.Extensions.Configuration 的众多配置源之一.但无论您将使用哪种提供程序,您最终都会得到具有部分和关键路径概念的相同配置格式.

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.

如果您在查看如何解析 JSON 时来到该包,那么您很可能看错了工具.如果您想解析 JSON 以检索正确的数据结构,就像您在使用 JSON 作为序列化数据的方式时使用的那样,那么您应该看看不同的东西.解析 JSON(也是序列化)最常见的解决方案是使用 Json.NET,这是一个非常强大且灵活的处理任何类型的 JSON 数据的工具.

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.

这篇关于Microsoft.Extensions.Configuration 究竟如何依赖于 ASP.NET Core?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆