我需要在 appsettings.json 中的两个位置使用数据库连接字符串。
是否可以在 json 文件中引入公共变量或 json-path 相关引用以避免潜在的问题?
如果能在不接触 C# 代码的情况下拥有它,那就太棒了。
{
...
"ConnectionStrings": {
"Default": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres"
},
"Nlog": {
"targets": {
"database": {
"type": "Database",
"dbProvider": "Npgsql.NpgsqlConnection, Npgsql",
"connectionString": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres",
...
}
}
...
}
Run Code Online (Sandbox Code Playgroud) 很简单的问题,我不确定我错过了什么。
\n我有一个环境秘密:
\n\n在这种情况下,“生产”环境可用于所有分支。\n我只是尝试在 appsettings 替换操作中访问 MESSAGE 变量(我尝试添加和删除存储库级别 MESSAGE 但无济于事):
\n- uses: actions/checkout@master\n- name: Variable\xe2\x80\xafSubstitution\n uses: microsoft/variable-substitution@v1\n with:\n files: DummyApi/appsettings.json\n env:\n Message: ${{ secrets.MESSAGE }}\nRun Code Online (Sandbox Code Playgroud)\n这将用 repo 级别设置替换它,但不是 env 级别设置,这根本不会替换它:
\n- uses: actions/checkout@master\n- name: Variable\xe2\x80\xafSubstitution\n uses: microsoft/variable-substitution@v1\n with:\n files: DummyApi/appsettings.json\n env:\n Message: ${{ env.MESSAGE }}\nRun Code Online (Sandbox Code Playgroud)\n简单地说 - 如何访问环境中的 MESSAGE 值?我需要手动设置环境吗?谢谢。
\n补充信息:
\n我真正想做的是这样的:
\non: [push]\njobs:\n build-and-deploy:\n runs-on: ubuntu-latest\n environment: contains(\'main\', $GITHUB_REF) ? production : development\nRun Code Online (Sandbox Code Playgroud)\n我知道最后一行不起作用,但似乎如果我设置环境,我可以看到秘密 - 我只想重用大量部署代码而不是复制/粘贴。
\n我有 .net core 5.0 应用程序并尝试从 appsettings.json 获取 DateTime
应用程序设置.json:
"TimeModel": {
"RestartDuration": "27.10.2021 12:30:00"
}
Run Code Online (Sandbox Code Playgroud)
代码:
services.Configure<TimeModel>(
configuration.GetSection("TimeModel"));
Run Code Online (Sandbox Code Playgroud)
错误:
System.InvalidOperationException: Failed to convert configuration value at 'TimeModel:RestartDuration' to type 'System.DateTime'.
---> System.FormatException: 27.10.2021 12:30:00 is not a valid value for DateTime.
---> System.FormatException: String '27.10.2021 12:30:00' was not recognized as a valid DateTime.
Run Code Online (Sandbox Code Playgroud) 我想使用我的web配置部分的configSource属性来定义各种设置的外部文件.
特别是appSettings部分.这意味着我的配置文件具有以下条目:
<appSettings configSource="My.AppSettings.config" />
Run Code Online (Sandbox Code Playgroud)
但是,如果更新此文件,则不会自动选择设置,如果将设置手动包含在web.config中,则会出现这种情况.
进一步调查引导我进入restartOnExternalChanges属性.这显然可以与<section/>元素一起使用来定义configSource标识的外部文件是否可以触发重启.大!或者我想.
但是,在尝试定义appSettings部分并更改restartOnExternalChanges值时,我看到此处遇到相同的错误,因为appSettings部分是在machine.config中定义的 - 我无法更改的文件.
对于已经在更高级别定义的部分,是否有人知道是否可以使这两个设置一起工作?
我有一个winforms应用程序,其中一些数据存储在XML文件中.应存储这些XML文件的位置可由用户配置,并存储在AppSettings中.我的所有图层都是单独的组件.我可以从我的DAL程序集中访问我的设置,还是应该将其作为参数传递到我的所有图层?
当我尝试从DAL层读取设置时,我遇到了另一个问题
Configuration config = ConfigurationManager.OpenExeConfiguration(
System.Reflection.Assembly.GetEntryAssembly().Location);
string dataStorageLocation = config.AppSettings["DataStorageLocation"];
Run Code Online (Sandbox Code Playgroud)
config.AppSettings ["DataStorageLocation"]给出编译错误:System.Configuration.ConfigurationElement.this [System.Configuration.ConfigurationProperty]由于其保护级别而无法访问.这是为什么?
有人能让我走上正轨吗?谢谢.
我正在使用ASP.NET 5,我想使用POCO类来访问我的appsettings.json文件.这个文件看起来像这样:
{
"Data": {
"ErpSystemConnection": {
"ConnectionString": "[myConnectionString]"
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"GoogleAnalytics": {
"Account": [
{
"Name": "AccountName",
"ServiceAccountEmailAddress": "someEmail@someaccount.iam.gserviceaccount.com",
"KeyFileName": "key1.p12",
"Password": "notasecret"
},
{
"Name": "AnotherAccount",
"ServiceAccountEmailAddress": "anotherEmailAccount@someotheraccount.iam.gserviceaccount.com",
"KeyFileName": "key2.p12",
"Password": "notasecret"
}
],
"KeyFilePath": "/googleApis/"
}
}
Run Code Online (Sandbox Code Playgroud)
"GoogleAnalytics"键包含一系列帐户,我希望这些帐户可以作为列表或数组在集合中访问.我创建了一个POCO来表示这个包含相应的'Account'对象集合的密钥:
public class GoogleAnalytics
{
public Account[] Account { get; set; } = new Account[1];
public string KeyFilePath { get; set; }
public GoogleAnalytics()
{
} …Run Code Online (Sandbox Code Playgroud) 如何检查appsettings.json.NET Core中是否存在配置节?
即使一个节不存在,以下代码也将始终返回实例化的实例。
例如
var section = this.Configuration.GetSection<TestSection>("testsection");
Run Code Online (Sandbox Code Playgroud) 我正在尝试从App.Config中读取应用程序名称。这是net461控制台应用程序。我已经添加了Serilog.Settings.AppSettings包。并且还在App.Config上添加了以下serilog配置
<add key="serilog:properties:Application" value="My App"/>
<add key="serilog:minimum-level" value="Debug" />
<add key="serilog:minimum-level:override:Microsoft" value="Debug" />
<add key="serilog:enrich:FromLogContext"/>
<add key="serilog:enrich:WithMachineName"/>
<add key="serilog:enrich:WithProcessId"/>
<add key="serilog:using:Seq" value="Serilog.Sinks.Seq"/>
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
<add key="serilog:using:Console" value="Serilog.Sinks.Console" />
<add key="serilog:write-to:Console"/>
Run Code Online (Sandbox Code Playgroud)
但是应用程序名称未显示在Seq上。但是,我appsettings.json为.netcore2项目添加了以下设置,该设置可以正常工作
"Serilog": {
"Properties": {
"Application": "Another app"
}
}
Run Code Online (Sandbox Code Playgroud)
缺什么?
我正在研究ASPNET核心Web API应用程序。当我尝试调试应用程序时,“ appsetting.json”文件未复制到调试文件夹。此刻,每当进行更改时,我都将复制此文件。如果我没有复制文件,而只是运行dotnet run命令,则应用程序加载失败,给文件未找到异常。
我应该在Launch.json文件中做什么吗?
这是我的东西。
我也尝试添加
"/appsettings.json":"${workspaceFolder}/appsettings.json"
Run Code Online (Sandbox Code Playgroud)
在"sourceFileMap"此JSON文件的属性中,但没有用。
请帮忙。
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 9222,
"webRoot": "${workspaceFolder}"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, …Run Code Online (Sandbox Code Playgroud) NET Core 2.x Web API项目模板提供了Program.cs和Startup.cs(默认情况下还有其他功能)。
如果您在的构造函数中放置一个断点Startup并添加一个监视,则可以看到从中加载的值appsettings.json。
Startup.cs并且Program.cs没有显式加载appsettings.json,因此它必须在调用CreateDefaultBuilder或Build中发生Program。
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)
我在https://github.com/aspnet/Configuration和https://github.com/aspnet/Hosting上查看了Microsoft源代码,尤其是WebHostBuilder,但是我看不到它发生的位置。
将代码加载到appsettings.json哪里?
appsettings ×10
asp.net-core ×4
json ×4
.net ×3
c# ×3
.net-core ×2
app-config ×1
asp.net ×1
configsource ×1
datetime ×1
github ×1
nlog ×1
serilog ×1
winforms ×1