我有一个非常简单的应用程序,我正在尝试使用 Settings 属性构建 app.config 。app.config 已构建,但运行时,键未检索值。
我最初使用的是从项目中添加应用程序配置文件并手动构建 app.config,并取得了巨大成功手动构建的 app.config 如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="Hosppath" value="C:\events\inbound"/>
<add key="Vendpath" value="C:\events\outbound"/>
<add key="Hospname" value="OBLEN_COB_Active_Inv_Advi_Daily_.csv"/>
<add key="Vendname" value="Advi_OBlen_Active_Inv_Ack_Daily_.csv"/>
<add key="Hospkey" value="invoice_number"/>
<add key="Vendkey" value="invoice"/>
<add key="Outpath" value="C:\events\Reports"/>
<add key="Outname" value="OB_Exception_Report_.xlsx"/>
<add key="Hospexcpt" value="O'Bleness Exceptions"/>
<add key="Vendexcpt" value="AdviCare Exceptions"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
调用类在这里:
using System;
using System.Configuration;
namespace LoadSettings
{
public static class ConfigReader
{
public static bool ReadAllSettings()
{
var success = false;
var appSettings …Run Code Online (Sandbox Code Playgroud) c# configurationmanager app-config appsettings application-settings
在控制台应用程序上使用 VS 2017。当我使用时Dictionary<string, Guid>一切正常,但是,当我使用Dictionary<Guid, Guid>它时它不会映射到 AppSettings 对象(字典计数为 0)。有什么办法可以解决这个问题,而不是在应用程序中使用string并将其转换为?Guid
应用程序设置.json
{
"AppSettings": {
"DictionaryTest": {
"55789653-86A3-485C-95E8-5E23C3219130": "74F87895-4965-447A-B07F-F702573218B7",
"FB1E7891-B3C2-4E83-A6BF-7F321C11BFFA": "FA7892A9-7925-4150-82A7-5DD3213D1242"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在程序.cs中
var appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($@"{appPath}\appsettings.json");
Configuration = builder.Build();
appSettings = new AppSettings();
Configuration.GetSection("AppSettings").Bind(appSettings);
Run Code Online (Sandbox Code Playgroud)
当我使用Dictionary<string, Guid>场景时,变量中有 2 个项目appSettings.DictionaryTest。
但是当我使用Dictionary<Guid, Guid>场景时,变量中有 0 个项目appSettings.DictionaryTest。
Dictionary<string, Guid>设想:
应用程序设置.cs
public class AppSettings
{
public Dictionary<string, Guid> DictionaryTest { get; set; …Run Code Online (Sandbox Code Playgroud) 我想从 appsettings.json 获取整个部分。
这是我的 appsettings.json:
{
"AppSettings": {
"LogsPath": "~/Logs",
"SecondPath": "~/SecondLogs"
}
}
Run Code Online (Sandbox Code Playgroud)
C#:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(this.SettingsFilesName);
configuration = builder.Build();
Run Code Online (Sandbox Code Playgroud)
此语法工作正常并返回 "~/Logs" :
configuration.GetSection("AppSettings:LogsPath");
Run Code Online (Sandbox Code Playgroud)
但是我怎么能拥有所有的“AppSettings”部分?是否可以?
此语法不起作用并且 value 属性为 null。
configuration.GetSection("AppSettings");
Run Code Online (Sandbox Code Playgroud)
更新:
我没有模型并在课堂上阅读它。我正在寻找这样的东西:
var all= configuration.GetSection("AppSettings");
Run Code Online (Sandbox Code Playgroud)
并像使用它一样
all["LogsPath"] or all["SecondPath"]
Run Code Online (Sandbox Code Playgroud)
他们把他们的价值观还给我。
我在 appsettings.json 文件中有一个节点,如下所示
"MyClassName": {
"MyList1": ["x","y"],
"MyList2": ["a","b","c"]
}
Run Code Online (Sandbox Code Playgroud)
为了保存和使用这些列表,我在后端有一个具有 2 个属性的类
public class MyClass
{
public List<string> MyList1 { get; set; }
public List<string> MyList2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有一种方法可以IConfiguration像这样使用填充这些属性
_iconfiguration.GetSection("MyClassName").Get<MyClass>();
Run Code Online (Sandbox Code Playgroud)
我需要在 .NET Core 应用程序中模拟上面的代码行。为了进行模拟,我使用 Moq(4.16.1) 我尝试像这样模拟它
_mockConfiguration
.Setup(m => m.GetSection(It.IsAny<string>()).Get<Myclass>())
.Returns(MyClassObject);
Run Code Online (Sandbox Code Playgroud)
但我显然错过了一些东西,因为我收到了不支持的表达式错误。错误说的是这样的
扩展方法(此处:ConfigurationBinder.Get)不得在设置/验证表达式中使用。
有办法实现这一点吗?
我正在尝试在 blazor 服务器应用程序的后台服务中访问存储在 appsettings.json 文件中的数据,但大多数有关如何在线执行此操作的插图都是基于 Blazor 的 Webassemble 配置。我想我应该使用 Microsoft.Extensions.Configuration 包,但我不太清楚如何实现它。
下面是后台服务及其基本代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using log4net;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
namespace BlazorServerApp
{
public class JobExecutedEventArgs : EventArgs { }
public class PeriodicExecutor : IDisposable
{
Timer _Timer;
bool _Running;
public void StartExecuting()
{
if (!_Running)
{
// Initiate a Timer
_Timer = new Timer();
_Timer.Interval = 5000;
_Timer.Elapsed += HandleTimer;
_Timer.AutoReset = true; …Run Code Online (Sandbox Code Playgroud) Trying to convert Dotnet Core 3.1 to Dotnet6 CONSOLE applications and keep getting
"The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was 'C:\Windows\system32\appsettings.json'."
error message. The thing is, the appsettings.json file IS copied to the publish folder, but I get the error in the Application log and nothing happens.
There are other posts here, but I can't find one specific to DotNet6 (they got rid of startup.cs and are doing things radically different) …
我正在尝试在特定端口(如 5000)上使用 ASP.NET Core 6 并行 REST API 运行 gRPC 服务器和客户端。我的意思是,我想使用 localhost:5000,但在检查了 的dotnetgRPC 模板之后,我发现Http2应该为 Kestrel 端点设置协议,appsettings.json如下所示:
{
"urls": "http://localhost:5000",
"Kestrel": {
"EndpointDefaults": {
"Protocols": "Http2"
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,我遇到了一个问题,我的 REST API 不再工作,我猜这是因为Http2提供给 Kestrel 端点的协议,因为当我删除提供的协议 API 时,它可以正常工作;相反,gRPC 客户端无法工作。有什么办法可以并行运行两个端点吗?
我正在使用C#(.Net 2.0)来开发Windows应用程序.我需要存储近50到60个用户设置.任何人,请告诉我哪个更好?
二进制序列化或应用程序设置(user.config)
谢谢.
我试图从appsettings.json读取,我不想使用DI.我只想访问该文件.我正按照MS的建议进行简单配置.
using System.IO;
using Microsoft.Extensions.Configuration;
class Program
{
public static IConfigurationRoot Configuration { get; set; }
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我指定引用Microsoft.Extensions.Configuration除了一个 - Configuration Builder实例创建时,类从红色突出显示变为绿色.它会产生以下错误.
错误CS0246找不到类型或命名空间名称'ConfigurationBuilder'(您是否缺少using指令或程序集引用?)
我发现的所有例子都以相同的方式设置.即使是文件Configuration Builder也说它已经过时了.我对如何进行感到茫然.
我正在使用.NET Core 2.0,如果它有任何意义的话.
如何string[]从IConfigurationRoot对象返回?
文件存在并设置为复制到输出
码
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", optional: false, reloadOnChange: false);
_configuration = builder.Build();
var arr = _configuration["stringArray"]; // this returns null
var arr1 = _configuration["stringArray:0"]; // this works and returns first element
Run Code Online (Sandbox Code Playgroud)
Settings.json
{
"stringArray": [
"myString1",
"myString2",
"myString3"
]
}
Run Code Online (Sandbox Code Playgroud) appsettings ×10
c# ×9
.net-core ×4
asp.net-core ×2
app-config ×1
asp.net ×1
blazor ×1
dictionary ×1
grpc ×1
grpc-dotnet ×1
json ×1
mocking ×1
moq ×1