简体   繁体   English

控制台应用程序看不到 Appsettings.Development.json .net core 3.1 配置问题

[英]Appsettings.Development.json do not seen by console app .net core 3.1 configuration problem

In my asp.net core 3.1 console app.在我的 asp.net core 3.1 控制台应用程序中。 In main class I have code like this:在主类中,我有这样的代码:

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();
    }
}

Environment set to Development环境设置为开发在此处输入图像描述

and config files like this (only values there differ):和这样的配置文件(只有值不同):

在此处输入图像描述

My app keeps taking values from appsettings.json.我的应用程序不断从 appsettings.json 获取值。 What to change in order to take values from appsettings.Developement.json?为了从 appsettings.Developement.json 获取值需要更改什么?

I also tried like this, but it didn`t work either:我也这样试过,但也没用:

    static void BuildConfig(IConfigurationBuilder builder)
    {
        builder.SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .AddJsonFile("appsettings.Development.json", optional: true)

            .AddEnvironmentVariables();
    }

Does anyone can help with that?有人可以帮忙吗? Files are properly copied to bin文件已正确复制到 bin 在此处输入图像描述

I just wanted to confirm that the DOTNET_ENVIRONMENT variable worked for me as well, but wanted to add that in Visual Studio 2022 in my.Net 6 Console App I had to configure the value in Launch Profiles, which I navigated to via the Debug Section in Project Properties:我只是想确认 DOTNET_ENVIRONMENT 变量对我也适用,但想在我的 .Net 6 控制台应用程序的 Visual Studio 2022 中添加它我必须在启动配置文件中配置值,我通过调试部分导航到项目属性: 在此处输入图像描述

I also did NOT need to add the appsettings.Develepment.json file to the builder when I tested this.当我测试这个时,我也不需要将 appsettings.Develepment.json 文件添加到构建器。

All I have in my Program.cs for configuring dependency injection, is this:我在 Program.cs 中用于配置依赖项注入的所有内容是:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Console.WriteLine("Starting...");

using var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((_, services) =>
    {
        services.AddTransient<ISqlServerTests, SqlServerTests>();
    })
    .Build();

Console.WriteLine("Done");

I found this on GitHub: https://github.com/aspnet/Hosting/issues/1440 .我在 GitHub 上找到了这个: https://github.com/aspnet/Hosting/issues/1440

I think the problem is that the ConfigurationBuilder isn't reading the launchsettings.我认为问题在于 ConfigurationBuilder 没有读取启动设置。 I added the following to fix that.我添加了以下内容来解决这个问题。

static async Task Main(string[] args)
        {
            var builder = new HostBuilder();
            builder
                .ConfigureWebJobs(b =>
                {
                    b.AddAzureStorageCoreServices();
                    //b.AddAzureStorage();
                    b.AddTimers();
                })
                .ConfigureHostConfiguration(configHost =>
                {
                    configHost.AddEnvironmentVariables(prefix: "ASPNETCORE_");
                    configHost.AddCommandLine(args);
                })
                .ConfigureAppConfiguration((hostingContext, config) => 
                {
                    var env = hostingContext.HostingEnvironment;

                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                })
                .ConfigureLogging((context, b) =>
                {
                    b.AddConsole();
                });
            var host = builder.Build();
            using (host)
            {
                // The following code ensures that the WebJob will be running continuously
                await host.RunAsync();
            }
        }

I had the same problem with my console application with default configuration providers.我的控制台应用程序与默认配置提供程序有同样的问题。 And the reason was the incorrect Environment variable like in your screenshot - ASPNETCORE_ENVIRONMENT.原因是屏幕截图中的环境变量不正确 - ASPNETCORE_ENVIRONMENT。 And I fixed it by replacing it by DOTNET_ENVIRONMENT:我通过用 DOTNET_ENVIRONMENT 替换它来修复它:

在此处输入图像描述

For.Net 6.0对于.Net 6.0

Add the environment variable DOTNET_ENVIRONMENT=Development添加环境变量 DOTNET_ENVIRONMENT=Development

Restart Visual Studio 2022.重新启动 Visual Studio 2022。

环境变量

what worked for me was setting the Copy to Output Directory to Copy if newer对我有用的是将Copy to Output DirectoryCopy if newer

.NET 3.1 - WPF app .NET 3.1 - WPF 应用程序

在此处输入图像描述

In .NET 5 and higher the setting is called DOTNET_ENVIRONMENT在 .NET 5 及更高版本中,该设置称为DOTNET_ENVIRONMENT

In your launchprofile.json you should see something like this在你的 launchprofile.json 你应该看到这样的东西

  "environmentVariables": {
    "DOTNET_ENVIRONMENT": "Development"
  }

You don't need this piece of code anymore你不再需要这段代码

 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
 .AddJsonFile("appsettings.Development.json", optional: true)

The hostbuilder will do this for you. hostbuilder将为您完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM