"Keyword not Supported" 使用环境变量从 Azure 应用程序设置中获取连接字符串时出现错误消息

"Keyword not Supported" Error message while fetching connection string from azure application setting using environment variable

我正在尝试使用 环境变量Azure 应用程序设置 获取连接字符串,但似乎连接的格式我在 Azure App 设置中放入的字符串不是 proper.Here 是我在本地主机中正常工作的原始连接字符串。

    <add connectionString="Server=tcp:****.database.windows.net,1433;Initial Catalog=***;
Persist Security Info=False;User ID=******;Password=*********;MultipleActiveResultSets=False;
    Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;
    Max Pool Size=500;Pooling=true;" name="Con_String"></add>

我将 "Server=tcp:****.database.windows.net,1433;Initial Catalog=****;Persist Security Info=False;User ID=****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=1200000;Max Pool Size=500;Pooling=true;" 作为 con_string 中的值 Azure Application Setting

现在为了在运行时获取连接字符串,我使用环境变量作为字符串 ConnectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_String");

但在 运行 网络应用程序期间,我收到异常消息 不支持关键字:'"服务器'。

我尝试了 Retrieve and use Windows Azure's connection strings? 和其他类似的帖子,但没有用。我在这里错过了什么愚蠢的东西吗?

Am I missing something silly here?

如果您仍然得到 Keyword not supported: '"Server'The ConnectionString property has not been initialized

我假设您在将连接字符串添加到 WebApp 应用程序设置后不保存应用程序设置

以下代码都在我这边工作。

ConnectionString = ConfigurationManager.ConnectionStrings["Con_String"].Connec‌​tionString.

ConnectionString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_Con_Stri‌​ng")

测试结果:

注意:正如 GauravMantri 提到的,Azure WebApp 应用程序设置中的连接字符串中不需要引号 (")。

在我的案例中,问题与愚蠢的错误有关:当 copy/pasted 连接字符串到 Notepad++ 时,我不知何故错过了换行符的出现,因为Notepad++ 正在换行文本或其他内容。

无论如何,我已经copy/pasted到Notepad然后替换了User IDPassword,然后再次copy/pasted到服务器,保存了设置然后重新启动服务器。 最后,问题解决了。