簡體   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