繁体   English   中英

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

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

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

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

和这样的配置文件(只有值不同):

在此处输入图像描述

我的应用程序不断从 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();
    }

有人可以帮忙吗? 文件已正确复制到 bin 在此处输入图像描述

我只是想确认 DOTNET_ENVIRONMENT 变量对我也适用,但想在我的 .Net 6 控制台应用程序的 Visual Studio 2022 中添加它我必须在启动配置文件中配置值,我通过调试部分导航到项目属性: 在此处输入图像描述

当我测试这个时,我也不需要将 appsettings.Develepment.json 文件添加到构建器。

我在 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");

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

我认为问题在于 ConfigurationBuilder 没有读取启动设置。 我添加了以下内容来解决这个问题。

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

我的控制台应用程序与默认配置提供程序有同样的问题。 原因是屏幕截图中的环境变量不正确 - ASPNETCORE_ENVIRONMENT。 我通过用 DOTNET_ENVIRONMENT 替换它来修复它:

在此处输入图像描述

对于.Net 6.0

添加环境变量 DOTNET_ENVIRONMENT=Development

重新启动 Visual Studio 2022。

环境变量

对我有用的是将Copy to Output DirectoryCopy if newer

.NET 3.1 - WPF 应用程序

在此处输入图像描述

在 .NET 5 及更高版本中,该设置称为DOTNET_ENVIRONMENT

在你的 launchprofile.json 你应该看到这样的东西

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

你不再需要这段代码

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

hostbuilder将为您完成此操作。

暂无
暂无

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

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