Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Configuration With Azure App Services and ASP.NET Core

DZone's Guide to

Configuration With Azure App Services and ASP.NET Core

See how you can configure a website with ASP.NET Core and put the finishing touches on it (and deploy it) through the cloud thanks to Microsoft Azure.

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

Most parts of elmah.io consist of small services. While they may not be microservices, they are in fact small and each does one thing. We recently started experimenting with ASP.NET Core (or just Core for short) for some internal services and are planning a number of blog posts about the experiences we have made while developing these services. This is the third part in the series about the configuration system available in Core. If you haven't already, read through AppSettings in ASP.NET Core and Config transformations in ASP.NET Core.

In the previous post, we saw how to switch configuration by specifying the ASPNETCORE_ENVIRONMENT environment variable. Let's try to deploy our website to Azure App Services. I've already created a new web app on Azure. To keep this post simple, I will deploy the website directly from Visual Studio, but in real life, you probably want to use Kudu, Octopus Deploy, Visual Studio Team Services, or something similar. Before deploying the website, we want to make sure that both appsettings.json and appsettings.Production.json are deployed. Add the production file in Project.json like this:

{
  ...
  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "appsettings.Production.json",
      "web.config"
    ]
  },
  ...
  }


Now we're ready to deploy the website. Right click the project and set up a new publish method:

Publish profile for ASP.NET Core

Click Publish and inspect the website hosted on Microsoft Azure:

ASP.NET Core hosted on Azure

Surprised how Azure automatically knows about appsettings.Production.json? In fact, it doesn't. Since we never told which environment we want to set in ASPNETCORE_ENVIRONMENT when running on IIS, Core defaults to the production environment.

You may have multiple environments running on Azure (like staging). There are two (probably more) ways to switch config values based on the current environment when running on Azure.

Using appsettings.json

The first approach extends the usage of the appsettings.json file. If you'd want to create a separate set of config values for your staging environment, simply create a file named appsettings.Staging.json (remember to add that file to Project.json as well):

{
  "AppSettings": {
    "Hello": "from staging"
  }
}


To tell Azure to use appsettings.Staging.json, add a new variable in the app settings section on your staging site:

Specifying the Staging variable on Azure

Azure automatically adds all app settings as environment variables as well, why Core picks up the right config file:

ASP.NET Core on staging

Using Application Settings

The other approach to switch config values based on environment, involves App settings on Azure as well. As you may know, specifying an app setting on an Azure web app, with the same name as an app setting located in the appSettings element in web.config, will actually override that value. But what about Core, which don't have app settings in web.config? App settings in Core are actually overridable as well, by specifying the full path to the key:

Override app setting on Azure

By using the : character, it is possible to reference variable names inside complex structures in appsettings.*.json files. In this example, the value from staging (specified in appsettings.Staging.json) is overwritten with the value from Azure:

ASP.NET Core website with overwritten value

That's it for Core configuration. In the next posts, we will take a look at something that we think is even more exciting: Logging.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
asp.net core ,cloud ,configuration ,website development ,tutorial

Published at DZone with permission of Thomas Ardal. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}