8

I have an app service in Azure operating as an API for a system I'm designing. As the API is responsible for accessing the database directly, I obviously don't want to be storing connection strings in the source code, so have stored it in the Connection Strings section within the App Service's Configuration on the Azure dashboard.

My code is pretty much a carbon copy of this >> https://github.com/medhatelmasry/JwtAuthentication/blob/master/JwtAuthentication/Startup.cs, except I have a check for the current configuration it's running in (debug, release, etc) so that when I'm debugging locally in Visual Studio I am using a localdb connection (hard coded). I have an appsettings.json file but there are no connection strings in it, only settings for JWT authentication and logging.

When this is being called:

services.AddDbContext<ApplicationDbContext>(
            option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

I'm getting the following in Azure:

Unhandled Exception: System.ArgumentNullException: Value cannot be null
Parameter name: connectionString

I've been working stupid hours over the past week trying to get this working and been going in circles, I'm driving myself to insanity. Google and StackOverflow results have been mixed as there are different answers from different versions of Azure and ASP.NET Core over the years. It's like it can't access the Azure configuration at all. Please refer to the link above as this is the same setup as I have, and there have been many different answers based on .NET versions and types (core or framework).

Edit: Please read my question, the connection string is not stored in the project's appsettings.json file, it's stored in Azure, as below (I've blanked the connection string names, but they do match what's in the code, and no it's not "DefaultConnection"):

Azure Connection Strings

16
  • Configuration.GetConnectionString() would require JSON like this: { "ConnectionStrings": { "DefaultConnection": "DB Connection String here" } }, how does your settings file look like? Commented Aug 22, 2019 at 10:19
  • 3
    Please read my question again, the connection string is not stored in the settings file, it's stored in Azure, as per Microsoft's guidance. Screenshot added. Commented Aug 22, 2019 at 10:26
  • Review the following stackoverflow.com/a/48959565/5233410 Commented Aug 22, 2019 at 10:33
  • 1
    You can view the environment variables of the app service in the Development Tools -> Advanced Tools section. That should prove whether or not the environment variable exists. It should be in the format of ConnectionStrings__DefaultConnection or ConnectionStrings:DefaultConnection (Windows app service only) Commented Aug 22, 2019 at 12:57
  • 1
    @DavidC799 you are an absolute DIAMOND!!!! It was the naming thing, if you put your answer I will accept it and give a few more details, I'm going to bed! Commented Aug 23, 2019 at 0:00

3 Answers 3

6

Make sure the naming of your variables are valid. You can view the actual environment variables from the SCM site at Development Tools -> Advanced Tools when in the App Service blade.

A connection string added via the portal Configuration section of the App Service with the name of "DefaultConnection" will look like SQLCONNSTR_DefaultConnection in the environment variables. To access it in code then, you would do configuration.GetConnectionString("DefaultConnection")

See this Microsoft blog for details on setting up the app service config values.

Sign up to request clarification or add additional context in comments.

9 Comments

This was the issue. My connection string had hyphens which seem to be not allowed (I'll presume anything non-alphanumeric isn't allowed). Say my connection string was "The-SQL-Server", the actual variable that was saved by Azure was "SQLCONNSTR_TheSQLServer", however "SQLCONNSTR_" is read and stripped out by Azure, so to call this from code you would use Configuration.GetConnectionString("TheSQLServer").
It's taken a very long time to figure this out and in all the Microsoft documentation I've read, there is no mention of disallowed characters for Azure variable names.
Yea I had a look through for restricted characters but couldn't find anything either. You're allowed colons and underscores for sure, anything else I would actively avoid!
It's poor, very poor. I can see my comment got edited, probably for the language, but I have been working stupid hours this week to get to the bottom of this, and am NOT happy with Microsoft at all, especially as Azure has actually stripped out what I've entered on the configuration itself but still kept the same name in the dashboard. To anybody reading this I'd say play it safe and just stick to alphanumeric with camel casing if needed (which is what I've done). Just got to fix the Managed Identity connection string that's broke now! dies inside a little
You doing it via KeyVault now then?
|
1

I know its an old one, but might be some use to someone in the future. My issue was caused by having the connection string in the AppService 'Connection Strings'. By putting it as a value in the 'Application Settings' instead it worked fine. I'm guessing because of the encryption that applies to the Connection String values.

Comments

0

I needed to include AddEnvironmentVariables() in the ConfigurationBuilder:

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.