When I deploy my Dot Net Core web app to Azure, the Environment.ContentRootPath variable is set to [myproject]/wwwroot. However, in development, it is just [myproject].
Why does it change in Azure deployment?
How can I make it consistent?
For reference, this is my IWebHost.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
var env = builderContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, true);
if (env.IsDevelopment())
config.AddUserSecrets<Startup>();
config.AddEnvironmentVariables();
})
.UseSetting("detailedErrors", "true")
.UseApplicationInsights()
.UseStartup<Startup>()
.CaptureStartupErrors(true)
.Build();
Use KUDU, you could find that your web contents are deployed to D:\home\site\wwwroot as follows:
By default, D:\home\site\wwwroot would be used as the content root and the host would search the content files (e.g. MVC view files,etc.) when hosting your application on Azure Web App.
In development, your app is started from the project's root folder, so the project's root folder is used as the content root.
Per my understanding, you may misunderstand the wwwroot folder in your project with the wwwroot folder on Azure Web App for storing your web content.
Moreover, you could use UseContentRoot to customize your content root folder. If the path doesn't exist, the host would fail to start. Details you could follow Hosting in ASP.NET Core.
Related
I have a Worker Service in Net Core 3.1 that reads Production settings from the appsettings.json file and, during development (using the "Debug" build configuration), overwrites appsettings.json settings with appsettings.Development.json, as expected.
I created a build configuration and publish configuration for our QA environment and I want that the appsettings.QA.json file to be merged with appsettings.json at build / publish time. But publishing the project only copies appsettings.json with the production settings without merging with the settings in the aspsetting.QA.json file.
How can I achieve this? I didn't want to copy appsettings.QA.json to the QA environment and set DOTNET_ENVIRONMENT to QA. I would like not to depend on environment variables in this case.
.Net Core does not merge the files into a physical file. They get merged in memory using such block of code in your Program.cs.
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
config.AddEnvironmentVariables();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
All you need to do is to set the environment variable ASPNETCORE_ENVIRONMENT and .net core will automagically take care of it.
I have a .NET Core 2.1 web API i am hosting locally with IIS. I have two child appSettings.json files, Development and Production. I have also created these environments within my project. My publishing profile uses Debug profile. When I actually debug my code it works perfectly fine.
The issue I have is that when I publish to IIS under a private IP and access it, it seems my code using the wrong appSettings.json file. I know this because I have different file paths used for different environments and it prints out the one from Production.
Even weirder when I physically delete appSettings.Production.json from my inetput/wwwroot/{MyProject} folder, then Postman cannot even talk to the API and I get a 500 bad request. Maybe I am not configuring or linking correctly? This is my first time working with environments.
I have lost about 4 hours on this and I am losing it...
Program.cs
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ConfigConfiguration)
.UseStartup<Startup>();
static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config)
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appSettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
}
Any more info required, do let me know!
Ensure to set the ASPNETCORE_ENVIRONMENT environment variable on the server, see Use multiple environments in ASP.NET Core
.
I have an ASP Core 2.2 application which runs fine.
I have an appsettings.json
I have an appsettings.QA.json
I have an Azure App Service which I have added ASPNETCORE_ENVIRONMENT variable to in the Configuration > App Settings section of the portal and set the value to "QA".
I have also added a connection string directly to the Configuration > Connection Strings section of the portal.
When I publish to my Azure App Service, I want to override the settings in appsettings.json with values from appsettings.QA.json. I am struggling to make sense of how this should be configured.
I am reading out the values from the appsettings via the Microsoft.Extensions.Configuration.IConfiguration implementation which is injected into classes where I need such configuration.
So far I have the following:
Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
})
.UseStartup<Startup>();
When the app runs in Azure, it is picking up the connection string from the portal and using it. However, when I read out a value from the appsettings, I always get the value from appsettings.json not appsettings.QA.json.
Can anyone point out how to configure the transforms correctly for an ASP Core 2.2 application?
Am I going about this in the wrong way entirely?
Update
I have removed my custom code above as suggested as it is not needed.
If I output #Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") in my view, I get: "QA;Development". It seems that the "Development" is coming from the web.config which also has ASPNETCORE_ENVIRONMENT set.
you shouldn’t read environment from appsettings. environment is set on machine level and is read at startup.
you have 2 options:
for local machine, set environment variable in launch.settings
in azure, set it in app service configuration as environment variable
ref: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2
I now have this working correctly:
Removed the custom code from the Program.cs as suggested by #KirkLarkin and #dee-zg.
Removed the ASPNETCORE_ENVIRONMENT setting from the Azure portal. The reason for this is that there is also a web.config in the app which has this setting. When reading from the config, it seems that this version is used, not the Azure portal version.
Why is the ASPNETCORE_ENVIRONMENT in the web.config? There is a discussion about this here. Ultimately, what I have done to work around this is to add transforms for the web.config.
I am using .Net Core2.2. I've an API project with environment variables. These variables are injecting on CI.
What I want to do is, when I run my integration tests, it should fire up the API project (or fake the exact API server) and call one of the controller.
The problem is environment variables on launchSettings.json are not injecting.
My test server initialization:
var testServer = new TestServer(new WebHostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
})
.UseStartup<Startup>());
I've added the same launchSetting.json file into test project but it did not worked also.
What I am doing wrong?
Thanks.
I want to deploy my C# API project in Microsoft Azure.
I've changed my wwwroot folder name to "Documents" like:
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseWebRoot("Documents")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<MyStartup>()
.Build();
host.Run();
}
It is working perfectly using POSTMAN or local server but when I'm accessing it with Microsoft Azure, It is showing error of not finding the above mentioned folder. How can I get it in Azure?
In Azure you have to go to Application setting and edit the path according to your requirements. Picture is attached for guidance.