"不支持关键字"使用环境变量从azure应用程序设置获取连接字符串时出现错误消息

时间:2018-01-18 06:00:40

标签: c# azure web-config environment-variables azure-application-settings

我正在尝试使用环境变量 Azure应用程序设置获取连接字符串,但似乎我在Azure App中放置的连接字符串的格式设置不正确。这是我的原始连接字符串在localhost中正常工作。

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

我在Azure Application Setting

中将"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

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

但在运行网络应用程序时,我收到异常消息不支持关键字:&#39;&#34;服务器&#39;

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

2 个答案:

答案 0 :(得分:1)

  

我在这里错过了一些愚蠢的东西吗?

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

在您添加WebApp appseting的连接字符串后,我假设您不保存appsetting

enter image description here

以下代码都在我身边。

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

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

测试结果:

enter image description here

注意:由于GauravMantri提到Azure WebApp appsetting中的连接字符串中不需要引号(&#34;)。

答案 1 :(得分:0)

在我的案例中,这个问题与一个愚蠢的错误有关:当将 copy / pasted 连接字符串复制到Notepad++时,由于Notepad++被包装,我以某种方式错过了换行符的出现文字或其他内容。

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