BrilBroeder
BrilBroeder

Reputation: 1579

asp.net core 2.2 is not using ConnectionString set in WebApp Azure config

I've asp.net core 2.2 apps deployed to Azure. In the Azure portal, I added a connection string for the app, but asp.net core is not picking up that one, it is using the one in appsettings.json.

What did I do to check this? I just copied the connection string I entered in Azure to the appsettings.json file (hence replacing the development version), redeployed it and it is working fine.

Any suggestions?

public class Program
{
    public static void Main(string[] args)
    {
        WebHost
            .CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build()
            .Run();
    }
}

Extract from startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
...
            services.AddDbContext<BattMobilityDbContext>(option => option.UseSqlServer(_configuration.GetConnectionString("BattMobilityDbConnection")));
...
}

Extract from appsettings.

  "ConnectionStrings": {
    "BattMobilityDbConnection": "Data Source=localhost\\SQLEXPRESS_JSL; initial catalog=BattMobility; integrated security=true"
  },

Screenshot from Azure

enter image description here

Upvotes: 0

Views: 1075

Answers (2)

BrilBroeder
BrilBroeder

Reputation: 1579

The solution was to add support for environmentvariables in OnConfiguring of the DbContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (_loggerFactory != null)
        {
            if (Debugger.IsAttached)
            {
                optionsBuilder.UseLoggerFactory(_loggerFactory);
            }
        }

        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
             // this was the missing part
            .AddEnvironmentVariables()

            .Build();
        var connectionString = configuration.GetConnectionString("xxx");

        optionsBuilder.UseSqlServer(connectionString);
    }

Upvotes: 2

Hans Olav
Hans Olav

Reputation: 449

I think I experienced the same thing when upgrading my azure functions to target ".NET Core 2.1". Suddenly the app didn't read AppSettings nor ConnectionString from the Azure App Service anymore.

I fixed it by adding the prefix "APPSETTINGS_" for AppSettings and "SQLAZURECONNSTR_" for Connection strings (AzureSQL type connection strings).

Here's the code for initializing the configuration root:

var basePath = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot")
    ?? $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";

var config = new ConfigurationBuilder()
        .SetBasePath(basePath)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: false)
        .AddEnvironmentVariables()
        .Build();

Here's the code for getting AppSetting and ConnectionString:

public static string GetAppSetting(string name)
{
    return ConfigurationRoot?[name]
        ?? ConfigurationRoot?["Values:" + name]
        ?? ConfigurationRoot?["APPSETTING_" + name]
        ?? ConfigurationManager.AppSettings[name];
}

public static string GetConnectionString(string name)
{
    return ConfigurationRoot?.GetConnectionString(name)
        ?? ConfigurationRoot?.GetConnectionString("SQLAZURECONNSTR_" + name)
        ?? ConfigurationManager.ConnectionStrings[name].ConnectionString;
}

Upvotes: 0

Related Questions