寻找任何与SlowCheetah合作的人在Web项目下转换配置文件的帮助.我们发现从web.config引用的部分配置文件没有被转换.
例如,我们在web.config中包含对部分配置AppSettings.config和ConnectionsString.config的引用,如下所示:
</system.web>
<connectionStrings configSource ="ConnectionsString.config"></connectionStrings>
<appSettings configSource ="AppSettings.config"></appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后在AppSettings.config中我们只有AppSettings部分,如下所示:
<appSettings>
<add key="LostPasswordBCC" value="knock@timmons.com" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
最后在转换文件AppSettings.Debug.config中我们有一些补充:
<?xml version="1.0" encoding="utf-8" ?>
<!-- For more information on using transformations
see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings >
<add key="Release" value="Something" xdt:Transform="Insert" />
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
显然上面只是一个测试,看看转换发生了,但我们发现的是,在尝试预览转换时,我们得到的所有内容都是错误"处理转换时出错".发布尝试也失败了.
如果我们使配置文件完全形成xml而不是从web.config引用,那么转换似乎工作正常 - 但是希望在多个项目之间共享这些文件.
有没有人知道是否有一个解决方法,我们既可以从web.config引用部分配置,也可以转换那些部分文件?我们正在处理遗留代码,其中包含大量尝试合并的Web项目中的配置文件,因此需要从Web配置链接到单独的共享文件.
我在 appsetting.json 中有这个提供程序字典
"AppSettings": {
"Providers": {
"http://localhost:5001": "Provider1",
"http://localhost:5002": "Provider2"
},
"ArrayWorks": [
"http://localhost:5001",
"http://localhost:5002"
],
"SoDoesColonInDictionaryValue": {
"Provider1": "http://localhost:5001",
"Provider2": "http://localhost:5002"
}
}
Run Code Online (Sandbox Code Playgroud)
下面抛出异常,因为字典键中有冒号。
Configuration.GetSection("AppSettings").Get<AppSettings>()
但是,冒号可以很好地用作字典值或数组,但不能用作字典键。我读到冒号在配置中具有特殊含义,但似乎没有办法逃脱。为什么?
编辑:
public class AppSettings
{
public string ApplicationName { get; set; }
public IDictionary<string, string> Providers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
调试 Configuration.GetSection("AppSettings") 时,您会得到这个
Key AppSettings:Providers:http://localhost:5000
Value Provider1
Run Code Online (Sandbox Code Playgroud)
本来是这样的
Key AppSettings:Providers:http_//localhost_5000
Run Code Online (Sandbox Code Playgroud)
但似乎没有办法控制配置如何对待 :::
我的启动中有以下设置:
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true);
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true)
Run Code Online (Sandbox Code Playgroud)
appsettings.json:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过添加具有不同值的匹配JSON结构来覆盖给定环境的有效设置,并省略我想要继承的那些,例如appsettings.Development.json:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
Run Code Online (Sandbox Code Playgroud)
但是我可以删除条目或部分,除非用空值覆盖每个值属性吗?
-S
下面是我面临的 Key-Vault 问题。 \n\xc2\xa0\n我有一个 Key-Vault,它存储一些在 Web 应用程序中使用的密钥,并使用 \n@Microsoft.KeyVault(SecretUri=)\ n\xc2\xa0\n秘密的值将使用自动化每三天更改一次。这也会更改版本号 (GUID),然后应用程序设置值(在“主要”和“辅助”情况下至少有一个)将变得无效。\n有什么方法可以在应用程序设置中引用 KeyVault 中的最新值吗?\更新所有网站中的应用程序设置将是一个乏味的过程。
\n当您从 Visual Studio (2017) 启动 ASP.NET Core 项目时,它假定工作目录是源代码所在的位置,而不是实际放置构建文件的位置。
这意味着当我运行我的项目时,它从 读取配置文件C:\Path\To\My\Project\appsettings.json,而不是从C:\Path\To\My\Project\bin\Debug\appsettings.json.
当我调试这段代码时,我可以看到情况是这样的:
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((context, config) => {
config.SetBasePath(context.HostingEnvironment.ContentRootPath);
config.AddJsonFile("appsettings.json", true, false);
});
Run Code Online (Sandbox Code Playgroud)
其中ContentRootPath指向项目文件夹,而不是放置构建文件的位置。
我可能可以通过设置Working Directoryin来解决这个问题Project Properties > Debug,但是由于我们还使用配置转换(SlowCheetah),每个开发人员都将拥有自己的调试输出构建配置(bin\Debug[CustomConfiguration]),并且.csproj对一个开发人员进行更改会破坏所有其他开发人员的配置。
有没有什么方法可以让 ASP.NET Core 从构建文件所在的位置而不是项目文件夹中读取配置文件,而无需更改Working Directory,但仍然适用于多个“构建配置”?
我在 Net Core 3.1 中有一个工作服务,它从appsettings.json文件中读取生产设置,并在开发过程中(使用“调试”构建配置)按预期appsettings.json用 覆盖设置appsettings.Development.json。
我为我们的 QA 环境创建了构建配置和发布配置,我希望在构建/发布时appsettings.QA.json合并该文件。appsettings.json但发布项目仅复制appsettings.json生产设置,而不会与文件中的设置合并aspsetting.QA.json。
我怎样才能实现这个目标?我不想复制appsettings.QA.json到 QA 环境并设置DOTNET_ENVIRONMENT为QA. 在这种情况下我不想依赖环境变量。
在我的 asp.net core 3.1 控制台应用程序中。在主类中我有这样的代码:
class Program
{
static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
BuildConfig(builder);
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddTransient<StartService>();
})
.Build();
var svc = ActivatorUtilities.CreateInstance<StartService>(host.Services);
svc.Run();
}
static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", optional: true)
.AddEnvironmentVariables();
}
}
Run Code Online (Sandbox Code Playgroud)
和这样的配置文件(只有值不同):
我的应用程序不断从 appsettings.json 获取值。为了从 appsettings.Developement.json 获取值,需要更改什么?
我也这样尝试过,但也没成功:
static void BuildConfig(IConfigurationBuilder builder)
{
builder.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.Development.json", optional: true)
.AddEnvironmentVariables();
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?文件已正确复制到 …
我的文件中有以下 JSON appsettings,它有一个连接字符串。
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ApplicationInsights": {
"InstrumentationKey": "8eje88w0-1509-4ae0-b58c-1a7ee5b9c7e4"
},
"ConnectionStrings": {
"DefaultConnString": "Server=MYPC\SQLEXPRESS;Database=CoreService.Mark;Trusted_Connection=True;"
},
"AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)
但是在执行程序时。我不断收到以下错误。我尝试查找任何错误,但找不到任何错误。请指教
Unhandled exception. System.FormatException: Could not parse the JSON file.
---> System.Text.Json.JsonReaderException: 'S' is an invalid escapable character within a JSON string. The string should be correctly escaped. LineNumber: 10 | BytePositionInLine: 42
Run Code Online (Sandbox Code Playgroud) 对于用户机密管理,我在开发阶段使用用户机密,并希望使用 Azure 密钥保管库进行发布和暂存。我有这个配置
"ConnectionStrings": {
"DefaultConnection": "MySecretConnectionString"
},
"SmtpSettings": {
"SmtpMailServer": "smtp.mailserver.somewhere",
"SmtpPort": "465",
"SenderLogin": "login",
"SenderDisplayName": "Site ",
"SenderPassword": "password",
"SenderEmail": "site@mailserver.somewhere",
"SecureSocketSettings": "SslOnConnect"
}
Run Code Online (Sandbox Code Playgroud)
当我想要在 Azure 密钥保管库中设置ConnectionStrings:DefaultConnection或存储时,就会出现问题。SmtpSettings:SenderPassword
有什么方法可以为嵌套属性分配和使用值吗?例如,就像我对用户机密所做的那样
dotnet user-secrets set "SmtpSettings:SenderPassword" "########" --project MyProject
Run Code Online (Sandbox Code Playgroud)
但对于 Azure 密钥保管库
az keyvault secret set --name "SmtpSettings:SenderPassword" --value "######" --vault-name MyProjectVault
Run Code Online (Sandbox Code Playgroud)
: 不允许,Parameter 'secret_name' must conform to the following pattern: '^[0-9a-zA-Z-]+$'.
connection-string appsettings azure azure-keyvault asp.net-core
我希望我的 .Net Core 5 控制台应用程序根据 DOTNET_ENVIRONMENT 环境变量从相应的 appsettings 文件中选择设置。我通过在 Visual Studio 2019 调试器中运行它并从 launchSettings.json 文件中获取环境来对此进行测试。
在 .Net Core 5 控制台应用程序中,我有 4 个“appsettings”文件:
每个文件属性都设置为“构建操作:内容”和“复制到输出目录:如果较新则复制”。
在我的 launchSettings.json 中,我将环境设置为“Staging”,如下所示:
{
"profiles": {
"MyAppName": {
"commandName": "Project",
"dotnetRunMessages": "true",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Staging"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要访问 Program.cs 中“Main”方法中的配置,因此在该类中我设置了一个模块级字符串变量“_environment”,如下所示在静态构造函数中:
_environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
Run Code Online (Sandbox Code Playgroud)
这有效;值“Staging”被加载到变量 _environment 中。
然后,我将配置加载到静态变量中,如下所示:(编辑 - 这是我的错误,假设此静态属性在静态构造函数之后加载。事实上,它在静态构造函数之前加载。这意味着未设置 _environment 变量,这意味着我的环境特定的应用程序设置文件从未加载)。
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{_environment}.json", optional: true, …Run Code Online (Sandbox Code Playgroud) appsettings ×10
asp.net-core ×5
c# ×5
.net-core ×2
slowcheetah ×2
.net ×1
.net-5 ×1
azure ×1
config ×1
json ×1
transform ×1
web-config ×1
xslt ×1