Microsoft.Extensions.Hosting EnvironmentName override default Production value - c#

var hostBuilder = new HostBuilder()
.ConfigureHostConfiguration((config) =>
{
config.AddEnvironmentVariables();
})
.ConfigureAppConfiguration((hostContext, config) =>
{
config.SetBasePath(Environment.CurrentDirectory);
config.AddJsonFile("appsettings.json", optional: false);
config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
config.AddEnvironmentVariables();
});
I add new global env variable in Docker file
ASPNETCORE_ENVIRONMENT = Development
but my hostContext.HostingEnvironment.EnvironmentName is always Production
How can I override EnvironmentName in a Microsoft.Extensions.Hosting with default Production value?

Open the json file launchsettings.json, and look for the section: "profiles" -> "your project name" -> "environmentVariables" -> "ASPNETCORE_ENVIRONMENT" and update this value from "Development" to "Production".

If it is an option you can also use and json config file:
var host = new HostBuilder()
.ConfigureHostConfiguration(builder =>
{
builder.AddJsonFile("hostsettings.json", optional: true);
})
{
"environment": "Development",
}

Related

How to configure environment on webjobs sdk?

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 can no longer get the values from local.setting.json with Azure Function

I was able to get and use the values from local.setting.json using Environment.GetEnvironmentVariable("test1");
now it's like there is no way to get this file and his values. I only know that another developer that isn't in my team changed something and from then doesn't works anymore... (I can't understand what he changes that can be the problem)
Actually for testing I tried this:
var appSettings = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var b = appSettings["test1"];
var c = appSettings["test2"];
var d = Environment.GetEnvironmentVariable("test1", EnvironmentVariableTarget.Machine);
var e = Environment.GetEnvironmentVariable("test1", EnvironmentVariableTarget.User);
var f = Environment.GetEnvironmentVariable("test1", EnvironmentVariableTarget.Process);
var g = Environment.GetEnvironmentVariable("test1");
After this all the variables are null
The local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"test1": "abcd",
"test2": "efgh"
}
}
I'm using visual studio 2017 and the project is Azure Functions, the file local.settings.json it's at the same level of the function.cs.
What could be?
Thank you!
Try the following:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", true, true)
.Build();
It may be the case that your context.FunctionAppDirectory may be incorrect. Please also right click the config file -> Configuration -> Advanced => set Copy to output directory to Copy If Newer.
In my case, the contents of the local.settings.json are read by the IConfigurationBuilder however, for some reason they're not loaded on the Environment variables list (Environment.GetEnvironmentVariables()) so the Environment.GetEnvironmentVariable will never read them. So I'm in my Startup class, I manually attached the variables to the current process.
Here's my local.settings.json
{
"Values" : { "Region": "southeastasia" }
}
In my Startup class, here's my IConfigurationRoot:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables().Build();
And here's how I attached them to the process:
Environment.SetEnvironmentVariable("Region", configuration.GetValue<string>("Values:Region"), EnvironmentVariableTarget.Process);
Now your Environment.GetEnvironmentVariable should look like this:
string region = Environment.GetEnvironmentVariable("Region", EnvironmentVariableTarget.Process);

Why do I get ERR_EMPTY_RESPONSE running nopCommerce 4 in Visual Studio with SSL?

When I try to run nopCommerce 4.0 locally in VisualStudio with SSL, I get the error ERR_EMPTY_RESPONSE. Running without HTTPS works fine. I am testing integration with an external login and I need to run HTTPS locally to avoid mixed content problems.
I know there are several SO posts about this, but they all lead me to the same place. My latest attempt is based on this article.
In my Startup.cs, I have:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc(
options =>
{
options.SslPort = 55391;
options.Filters.Add(new RequireHttpsAttribute());
}
);
services.AddAntiforgery(
options =>
{
options.Cookie.Name = "_af";
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.HeaderName = "X-XSRF-TOKEN";
}
);
return services.ConfigureApplicationServices(Configuration);
}
In certificate.json, I have:
{
"certificateSettings": {
"filename": "localhost.pfx",
"password": "secretpassword"
}
}
In Program.cs, I have:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
.AddJsonFile($"certificate.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
.Build();
var certificateSettings = config.GetSection("certificateSettings");
string certificateFileName = certificateSettings.GetValue<string>("filename");
string certificatePassword = certificateSettings.GetValue<string>("password");
var certificate = new X509Certificate2(certificateFileName, certificatePassword);
var host = WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Loopback, 55391, listenOptions =>
{
listenOptions.UseHttps(certificate);
});
options.Listen(IPAddress.Loopback, 55390);
})
.UseStartup<Startup>()
.CaptureStartupErrors(true)
.Build();
host.Run();
}
I have ASPNETCORE_HTTPS_PORT set to 55391 in my Nop.Web project settings.
When I run this, I get the error ERR_EMPTY_RESPONSE.
If I remove all of the HTTPS options and just go with the standard setup, everything works fine.
Edit: I have to run this project with the Nop.Web profile, so I can't run it with IIS Express and just get the SSL settings in the project properties.
Edit #2: I noticed that when I get this error, the browser is being redirected from https://localhost:55391 to http://localhost:55391 (no s). I'm not sure why.

.NET IConfiguration returns null for a property

My breakpoints were out of sync so I had to change my launch.json's program property to point at the new folder that .NET2.0.0 creates.
Now when my app tries to get a property from the app.Development.json file it ends up being null:
`
.ConfigureAppConfiguration((hostContext, config) =>
{
// delete all default configuration providers
hostCtx = hostContext;
var hostConf = hostContext.Configuration;
var env = hostContext.HostingEnvironment;
config.Sources.Clear();
config.SetBasePath(env.ContentRootPath);
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
// these both are null
certFileName = hostConf["AppConfiguration:CertFileName"];
certPassword = hostConf["AppConfiguration:CertPassword"];
})
`
It looks like if I do:
var c = config.Build();
And use c instead of hostConf it works.
I'm not sure why though since I thought the hostContext would contain the Configuration already. If anyone knows why or a cleaner way, I'm all ears.

Why does custom urls work with UseConfiguration and not with ConfigureAppConfiguration?

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?

Categories

Resources