2017-10-30 5 views
2

Ich versuche Appsettings Transformation zu einer .net-Core-2-Konsole-Anwendung hinzuzufügen, z..Net Core 2 Konsole Appsettings transformieren

  • appSettings.json
  • appSettings.Test.json
  • appSettings.Prod.json

ich den folgenden Code gefunden haben, für asp.net Kern funktioniert:

public Startup(IHostingEnvironment env) 
{ 
    var builder = new ConfigurationBuilder() 
     .SetBasePath(env.ContentRootPath) 
     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true) 
     .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: 
true) 
     .AddEnvironmentVariables(); 
    Configuration = builder.Build(); 
} 

Ich weiß jedoch nicht, wie man env.EnvironmentName bekommt, weil es keine IHostingEnvironment in einer Konsolen-App gibt.

Jede Hilfe wird

+0

Mögliche Duplikat [jetzt klar sein, Verwenden von Start up-Klasse in ASP.NET5-Konsolenanwendung] (https://stackoverflow.com/questions/30257710/using-startup-class-in-asp-net5-console-application#30259936) – CalC

+2

Dies ist kein Duplikat, da IApplicationEnvironment jetzt hat wurde entfernt https://github.com/aspnet/PlatformAbstractions/issues/37 –

+2

Und 'Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application' enthält keine Umgebungsinformationen –

Antwort

0

alles so mit Präprozessor finden konnte nicht anders Richtlinien

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

public Startup() 
{ 
    var builder = new ConfigurationBuilder() 
     .SetBasePath(env.ContentRootPath) 
     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true) 
#if RELEASE 
 .AddJsonFile($"appsettings.Release.json", optional: 
true) 
     .AddEnvironmentVariables(); 
    Configuration = builder.Build(); 
} 
Verwandte Themen