2012-04-28 85 views
2

我正在編寫WsCF服務以檢索多個客戶端的數據。我有一個引用業務管理器對象的服務層。實質上,在服務調用中,我想檢索提供程序類型(我的代碼中的自定義枚舉)以及web.config中的連接字符串。IIS Express引用machine.config與項目web.config

出於某種原因,代碼無法找到我的項目web.config中的設置。我正在收集IIS Express,VS開發Web服務器正在引用machine.config。我如何覆蓋這個。這是非常令人沮喪的。正如你可以從片段中看到的設置AND連接字符串名稱存在,但代碼從來沒有拿起它。

我的代碼:

Private Sub GetDBInformation() 
    Dim config As System.Configuration.Configuration 
    config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Nothing) 
    If (config.AppSettings.Settings.Count > 0) Then 
     Dim Setting As System.Configuration.KeyValueConfigurationElement 
     Setting = config.AppSettings.Settings("DB TYPE") 
     If Not (Setting.Value = Nothing) Then 
      Select Case Setting.Value 
       Case "SQL SERVER" 
        provider = ProviderType.SqlServer 
       Case Else 
        provider = ProviderType.OleDb 
      End Select 
     End If 
    End If 

    If (config.ConnectionStrings.ConnectionStrings.Count > 0) Then 
     connection = config.ConnectionStrings.ConnectionStrings("UMADB").ConnectionString 
    End If 
End Sub 

的Web.config:

<connectionStrings> 
    <add name="UMADB" 
     connectionString="Data Source=***********;Initial Catalog=UMA;UserID=********;Password=***********" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 

<appSettings> 
    <add key="DB TYPE" value="SQL SERVER" /> 
</appSettings> 

回答

0

這是訪問的配置設置的一個非常迂迴的方式。我不知道你爲什麼或如何進入machine.config,但有一個更簡單的方法。

不要忘記導入System.Configuration命名空間。

Dim Setting As String = ConfigurationSettings.AppSettings.Get("DB_TYPE") 
If Not String.IsNullOrEmpty(DatabaseSetting) Then 
    Select Case Setting 
     Case "SQL SERVER" 
      provider = ProviderType.SqlServer 
     Case Else 
      provider = ProviderType.OleDb 
    End Select 
End If  

我不知道如何或爲何你到了machine.config,但是這不應該發生。使用您目前的方法,請嘗試在獲取配置文件時指定應用程序根目錄:

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/") 
相關問題