Azure无法访问存储在应用程序服务配置中的连接字符串 [英] Azure cannot access connection string stored in app service configuration

查看:100
本文介绍了Azure无法访问存储在应用程序服务配置中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Azure中有一个应用程序服务,它作为我正在设计的系统的API.由于API负责直接访问数据库,因此我显然不想将连接字符串存储在源代码中,因此将其存储在Azure仪表板上的App Service的配置"中的连接字符串"部分中.

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.

我的代码几乎是此代码的副本>> https ://github.com/medhatelmasry/JwtAuthentication/blob/master/JwtAuthentication/Startup.cs ,除了我要检查它正在运行的当前配置(调试,发行等)时,以便在Visual Studio中进行本地调试我正在使用localdb连接(硬编码).我有一个appsettings.json文件,但其中没有连接字符串,只有JWT身份验证和日志记录的设置.

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.

被称为:

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

我在Azure中得到以下内容:

I'm getting the following in Azure:

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

在过去的一周里,我一直在愚蠢的工作,试图使它正常工作,并且一直在圈子里盘旋,我正使自己陷入精神错乱.多年来,Google和StackOverflow的结果好坏参半,因为不同版本的Azure和ASP.NET Core有不同的答案.就像它根本无法访问Azure配置一样.请参考上面的链接,因为这与我的设置相同,并且根据.NET版本和类型(核心或框架)有很多不同的答案.

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).

请阅读我的问题,连接字符串未存储在项目的appsettings.json文件中,而是存储在Azure中,如下所示(我已将连接字符串名称空白,但它们确实与代码中的内容匹配,不,它不是"DefaultConnection"):

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

推荐答案

确保变量的命名有效.当位于App Service刀片中时,可以从SCM站点的开发工具"->高级工具"中查看实际的环境变量.

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.

通过App Service的门户网站配置"部分添加的名称为"DefaultConnection"的连接字符串在环境变量中看起来类似于SQLCONNSTR_DefaultConnection.要在代码中访问它,您可以执行configuration.GetConnectionString("DefaultConnection")

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.

这篇关于Azure无法访问存储在应用程序服务配置中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆