Azure cannot access connection string stored in app service configuration

梦想与她 提交于 2019-12-22 10:20:54

问题


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"):


回答1:


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.



来源:https://stackoverflow.com/questions/57606636/azure-cannot-access-connection-string-stored-in-app-service-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!