Azure Web Apps and ASP.NET5 Configuration

In one of my prior posts, I covered how to deal with SQL Azure with an Azure App Service Web App. I described how to create the SQL Azure database and how to set up an app setting to tell your NodeJS application where the SQL Azure database was.

What about ASP.NET5?

Well, it turns out that the AddEnvironmentVariables() method takes care of it for you, if you know where to look. Take a look at my Startup.cs file:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.AspNet.Hosting;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration;
using System;

namespace AspNetSqlConnStr
{
    public class Startup
    {
        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            var cb = new ConfigurationBuilder(appEnv.ApplicationBasePath);
            cb.AddJsonFile("config.json", optional: true);
            cb.AddEnvironmentVariables();

            Configuration = cb.Build();

        }

        public IConfiguration Configuration
        {
            get;
            private set;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            // The SQLCONNSTR_MS_TableConnectionString becomes Data:MS_TableConnectionString
            var dataSource = Configuration.GetSection("Data:MS_TableConnectionString");
            var strings = dataSource.GetChildren();
            Console.Out.WriteLine("dataSource found!");
        }

        public void Configure(IApplicationBuilder app)
        {
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

This is ASP.NET5 Beta6 code, so it may be slightly different if you are using a different beta release. Create a new ASP.NET Application and add the highlighted lines. Put a breakpoint on the Console line (#33) so that you can take a look at the data.

Next, set a value for SQLCONNSTR_MS_TableConnectionString. From my prior post, this is an app setting in the Azure Portal and appears as an environment variable when the application is run. You can do this in Visual Studio by right-clicking on the project and selecting Properties. Under the Debug tab is a place for environment variables:

09222015-pic1

The actual value doesn’t matter at this point – it just has to be something. However you need to ensure it is a valid SQL Server connection string when you are actually using it.

Now you can run that application and check out the output and the breakpoint:

09222015-pic2

Note that the dataSource has two elements – the Data:MS_TableConnectionString:ProviderName contains the class that you need to instantiate to connect to it – this works for MySQL (with a prefix of MYSQLCONNSTR) as well as others.

So, how do you use this? Well, probably the easiest way is to grab the connection string like this:

var connectionString = Configuration.GetSection("Data:MS_TableConnectionString:ConnectionString").Value;

You can then use this string as part of the Entity Framework setup. You could also do something like the following for injection into other objects:

        public void ConfigureServices(IServiceCollection services)
        {
            // The SQLCONNSTR_MS_TableConnectionString becomes Data:MS_TableConnectionString
            services.Configure<DbSettings>(Configuration.GetSection("Data:MS_TableConnectionString"));
            Console.Out.WriteLine("dataSource found!");
        }

The DbSettings class looks like the following:

namespace AspNetSqlConnStr.Models
{
    public class DbSettings
    {
        public string ConnectionString { get; set; }
        public string ProviderName { get; set; }
    }
}

You can now ask for this in your controllers and other code using dependency injection – just like normal.