I was struggling to use custom urls for Kestrel and I finally found a combination of settings and builders that work but I'm really confused why does the following work:
var configuration =
new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true, reloadOnChange: true)
.Build();
var host =
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(contentRootPath)
.UseConfiguration(configuration)
.UseStartup<Startup>()
.Build();
but not this:
var host =
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(contentRootPath)
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile("hosting.json", optional: true, reloadOnChange: true);
})
.UseStartup<Startup>()
.Build();
It just ignores the hosting.json file as if it wasn't there.
What is the difference between using your own builder vs. using the ConfigureAppConfiguration extension? Is there a rule of thumb when to use which?
Related
I have a dotnet console app using webjobs sdk and I´m not able to find how to get the configuration file correctly based on the environment. My code:
static void Main(string[] args)
{
var builder = new HostBuilder();
var environmentName = Environment.GetEnvironmentVariable("environment");
builder.ConfigureHostConfiguration(config =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
});
...
After that I create 2 files: appsettings.json and appsettings.Production.json. When I´m debugging, even with the variable set to production, I always get the appsettings.json values and not the appsettings.Production.json value. What Im doing wrong here?
I have developed a web API and published it on azurewebsites.net.
I have added the following additional appsettings:-
appsettings.Dev.json
appsettings.Test.json
appsettings.Prod.json
To be able to extract values from these appsettings transforms I made the following code changes:
Tried the solution mentioned here:
https://stackoverflow.com/a/44953524/10485667
Even tried using only the Development/Debug, Staging and Production/Release instead of Dev, Test, Prod receptively. But no luck. It would only publish the values from the main appsettings.json.
Startup.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
#if DEBUG
.AddJsonFile($"appsettings.Dev.json", optional: true)
#endif
.AddEnvironmentVariables();
Configuration = builder.Build();
appSettings = Configuration.Get<AppSettingsModel>().AppSettings;
}
even tried this code:
AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
Tried changing the Program.cs:
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile($"appsettings.Development.json", optional: true)
.Build();
ILogger logger = null;
var host = CreateWebHostBuilder(args)
.UseConfiguration(config)
.Build();
logger = host.Services.GetService<ILogger>();
host.Run();
}
}
Tried every possible solution provided on internet but no luck. After publishing to azure, it takes the values only from appsettings.json
I think I might be making some conceptual mistake while attempting these solutions. Any kind of help is appreciated.
Thanks in advance
TraceSource is accessible from System.Diagnostics in my ASP .NET Core project.
In src file you can find header:
#region Assembly System.Diagnostics.TraceSource, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.Diagnostics.TraceSource.dll
#endregion
What does it mean? Is Version of .Net Famework >=4.1.1.0 acceptable? Is TraceSource included in some version of .Net Standard?
UPD MY RESOLUTION:
It need configuration.
1) app.config works only for .NET Framework, https://github.com/dotnet/corefx/issues/24829
2) Draft for .Net Core:
TraceSource.Listeners.Add(new MyListener());
TraceSource.Switch = new SourceSwitch();
This snippet may help you out.
public static void Main(string[] args)
{
var webHost = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
.UseStartup<Startup>()
.Build();
webHost.Run();
}
you can also follow this link for an in-depth guide regarding logging in dotnet core.
I want to read environment from command line args and use its value to build Configuration itself. Environment could be passed by different ways (--environment Staging, -e=Staging etc.) so I do not want to parse args myself. I found that we can first build config to read only environment and than build whole configuration:
var envName = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build()
.GetValue<string>("Environment");
var contentRoot = Directory.GetCurrentDirectory();
var configuration = new ConfigurationBuilder()
.SetBasePath(contentRoot)
.AddJsonFile($"Configuration/appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"Configuration/appsettings.{envName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
Is there better way to do this?
I am creating some tests using TestServer which is bootstrap with a complex configuration as following:
var config = new ConfigurationBuilder()
.Build();
webHostBuilder = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.CaptureStartupErrors(true)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<MockLicenseStartup>()
.UseEnvironment("Development")
.UseUrls("http://locahost");
testServer = new TestServer(webHostBuilder);
Both in my "asp.net core" project and in my test project I have created multiple appsettings.json which is used to provide things like:
Connection String
Log verbosity
Custom Sections
The issue I am facing is that my Configuration class, inside the MockLicenseStartup is not able to load any of the available appsettings.json.
The code used inside MockLicenseStartup.cs is this one:
public MockLicenseStartup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
When I call Configuration.GetConnectionString("") it throws an exception and if I inspect further I can see that no configuration has been loaded actually. Probably is a problem related to the relative/absolute path of .UseContentRoot(Directory.GetCurrentDirectory())
In Test Environment,
.SetBasePath(env.ContentRootPath)
env.ContentRootPath is different from production, it is set to the test project's bin directory if I remember correctly. So, it will not locate the appsettings.json file. unless you copy it there after build.
If you are projects folder structure does not change. you can just try to hard code the appsettings.json" path in these two lines to where these are located.
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true
if this works (it should), you can make it better by find the appsetting.json path in code.
Below is my own code which work in test environment.
var settingFilePath = getSettingFilePath(settingFileParentFolderName: "APIProject");
var builder = new ConfigurationBuilder()
.AddJsonFile(settingFilePath + _settingFileName, optional: true, reloadOnChange: true)
.AddJsonFile(settingFilePath + "appsettings.Development.json", optional: true);
var configuration = builder.Build();
getSettingFilePath() is just a function to locate the setting file path in the Startup Project folder.
Hope this help.