[英]How do I get the usersecrets file to load when running a console app from Visual Studio 2019
This must be a stupid questions..这一定是个愚蠢的问题..
In my launchsettings.json
I have tried在我的
launchsettings.json
我试过
"environmentVariables": {
"environment": "Development",
"ASPNETCORE_ENVIRONMENT": "Development",
"NETCORE_ENVIRONMENT": "Development"
}
And when I look at the "environment provider" these values exists in the environment.当我查看“环境提供者”时,这些值存在于环境中。
Trying to use the UserSecrets
feature, but the docs says that this file will only be added if the environment is set to Development
.尝试使用
UserSecrets
功能,但文档说只有在环境设置为Development
才会添加此文件。
And looking at the看着
HostBuilderContext.HostingEnvironment.EnvironmentName
It says "Production".. where can I set this value.......argh.. :(它说“生产”......我可以在哪里设置这个值.......啊...... :(
I'm not able to find any documentation on this.我找不到任何关于此的文档。 But probably I'm missing something simple.
但可能我错过了一些简单的东西。
Some more info更多信息
Program.cs程序.cs
using Microsoft.Extensions.Hosting;
using System;
namespace TestEnv
{
class Program
{
static void Main(string[] args)
{
var builder = CreateHostBuilder(args).Build();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).ConfigureServices((HostBuilderContext, services) =>
{
Console.WriteLine(HostBuilderContext.HostingEnvironment.EnvironmentName);
});
}
}
launchsettngs.json启动设置.json
{
"profiles": {
"TestEnv": {
"commandName": "Project",
"environmentVariables": {
"ENVIRONMENT": "Development"
}
}
}
}
The output HostBuilderContext.HostingEnvironment.EnvironmentName
is Production
输出
HostBuilderContext.HostingEnvironment.EnvironmentName
是Production
launchsettings.json启动设置.json
{
"profiles": {
"TestEnv": {
"commandName": "Project",
"commandLineArgs": "environment=Development",
"environmentVariables": {
}
}
}
}
The output HostBuilderContext.HostingEnvironment.EnvironmentName
is Development
输出
HostBuilderContext.HostingEnvironment.EnvironmentName
是Development
And the UserSecrets file is loaded.并加载 UserSecrets 文件。
The structure of a launchSettings.json
: launchSettings.json
的结构:
{
"profiles": {
"My.ConsoleApp": {
"commandName": "Project",
"environmentVariables": {
"ENVIRONMENT": "Development"
}
}
}
}
Or in Visual Studio right click on your project (not solution) and choose properties.或者在 Visual Studio 中右键单击您的项目(不是解决方案)并选择属性。 From left menu pick Debug and then you also can write the Environment variables.
从左侧菜单中选择调试,然后您还可以编写环境变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.