2014-10-31 113 views
5

我已在azure網站上傳了一個azure webjob。我試圖從azure網站讀取連接字符串,其中azure webjob使用下面的代碼,但它不返回任何內容。 Azure網站中的連接字符串位於Azure網站的配置選項卡中。我在這裏做錯了嗎?從Azure網站連接字符串配置中讀取連接字符串

CloudConfigurationManager.GetSetting("ConnString") 

感謝

回答

6

的CloudConfigurationManager.GetSetting( 「CONNSTRING」)正在尋找內Azure中的應用程序設置,而不是你的連接字符串一個setting with the key 「CONNSTRING」。

您需要爲您的網站含有添加的應用程序設置的值,你的連接字符串

enter image description here

+4

我將其更改爲ConfigurationManager.ConnectionString [「ConnString」],這也給了我值。感謝你及時的答覆 。 – user505210 2014-10-31 14:14:13

+0

User505210 - 你應該張貼,作爲一個答案,我會給予好評吧:) – samneric 2016-10-16 17:59:08

0

爲了讓應用程序設置或連接字符串使用普通的.NET API

System.Configuration.ConfigurationManager.AppSettings["name"]System.Configuration.ConfigurationManager.ConnectionStrings["name"]

CloudConfigurationManager用於Web角色/輔助角色。

+0

的CloudConfigurationManager.GetSetting作爲一種抽象,在Azure上運行它會變得湛藍的應用程序設置,運行本地將訪問的web.config – 2014-10-31 22:03:19

+3

當您正在使用Azure網站PAAS解決方案,因爲您只需從本地,IIS服務器和Azure中都能正常工作,就可以獲得適當的行爲,無需冗餘抽象。您也不需要引用額外的程序集,並且CloudConfigurationManger也沒有連接字符串支持。 – 2014-11-01 17:57:13

+0

絕對地,ConfigurationManager.AppSettings可以同時處理web.config和azure設置,因此不需要使用CloudConfigurationManager.GetSetting。 – 2014-11-08 22:18:12