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

查看:47
本文介绍了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(推荐)配置.鉴于此,我添加了三个必要的块包:

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< T>()?

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.Configuration Microsoft.AspNetCore.App 的一部分或在很大程度上依赖于 Microsoft.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 Core.这就是为什么它们位于 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 base 库,该库没有任何加载文件的功能,因此您还需要

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天全站免登陆