2013-06-18 51 views
9

我有一個使用不同程序集的函數的Web.Api應用程序。對於這個程序集,我創建了一個.config文件,用於存儲一些字符串。dll.config未複製到臨時asp.net文件文件夾

我使用下面的代碼應取這些字符串之一:

private static string LogUrl = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings["WebApi-LogUrl"].Value.ToString(); 

Assembly.GetExecutingAssembly().Location點臨時asp.net文件,(C:\ WINDOWS \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Temporary ASP.NET Files \ root \ dc2fa3d4 \ 834ee436 \ assembly \ dl3 \ cd068512)但我的dll.config文件沒有複製到那裏。 結果是,我無法調試我的應用程序,並且它在真實的IIS服務器上運行代碼時也爲空。

如果我在獲取設置之前設置了一個斷點,我可以獲得臨時文件夾,並且當我將dll.config文件複製到此處時,它可以工作,但我應該如何自動執行該操作。

我的屬性設置爲我的dll.config文件「建設行動:內容」,「複製到輸出目錄:總是」

任何幫助,將不勝感激,用Google搜索了好幾個小時了。 !

// The dllPath can't just use Assembly.GetExecutingAssembly().Location as ASP.NET doesn't copy the config to shadow copy path 
var dllPath = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath; 
var dllConfig = ConfigurationManager.OpenExeConfiguration(dllPath); 

// Get the appSettings section 
var appSettings = (AppSettingsSection) dllConfig.GetSection("appSettings"); 
return appSettings.Settings; 

的關鍵有::(

最好的問候, 彼得·拉爾森

回答

17

我用下面的代碼解決了這個

new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath 

我想出了閱讀後的解決方案Zhaph - Ben Duguid的答案在這裏:https://stackoverflow.com/a/2434339/103778

現在我可以拿到我的網絡應用程序的bin目錄中的DLL配置文件。

我從written up a blog post開始討論這個問題。

+0

啊哈,我得檢查一下......謝謝你的建議! –

+0

爲我工作。謝謝你的提示! –

+0

這幫了我很多。我還注意到,對GetName()的調用是無用的,因爲屬性CodeBase也是類Assembly的一個屬性。 – sluki

相關問題