2010-06-30 62 views
6

我在的形式a​​pp.config文件:如何讀取app.config中定義的屬性的值?

<?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
    <system.serviceModel> 
     <client> 
     <endpoint address="http://something.com" 
     binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer" 
     contract="ABC" name="XXX" /> 
     <endpoint address="http://something2.com" 
     binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransfer" 
     contract="ABC2" name="YYY" /> 
     </client> 
    </system.serviceModel> 
    </configuration> 

我想在屬性讀取值具有名稱=「XXX」節點端點的「地址」。請告訴我該怎麼做!

(繼續在下面討論marc_s。抱歉,因爲註釋不允許格式化代碼,所以將文本放在這裏) @marc_s:我使用下面的代碼來讀取上面的文件,但它顯示clientSection.Endpoints有0成員(Count = 0)。請幫忙!

public MainWindow() 
    { 
     var exeFile = Environment.GetCommandLineArgs()[0]; 
     var configFile = String.Format("{0}.config", exeFile); 
     var config = ConfigurationManager.OpenExeConfiguration(configFile); 
     var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config); 
     var clientSection = wcfSection.Client; 
     foreach (ChannelEndpointElement endpointElement in clientSection.Endpoints) 
     { 
      if (endpointElement.Name == "XXX") 
      { 
       var addr = endpointElement.Address.ToString(); 
      } 
     } 
    } 
+0

你不應該使用* vshost.config」文件 - 這是隻打算如果從運行您的程序是存在在Visual Studio中使用'MyApp.exe.config'而不是!! – 2010-07-01 14:37:33

+0

@marc_s:嗨,我已經更新了我的代碼,但是讀取的端點數仍然爲零。 – 2010-07-02 03:01:17

+0

是的,我可以看到 - 用我的代碼但不要使用'ServiceModelSectionGroup.GetSectionGroup(config);'和下面幾行,而是使用我的代碼('ConfigurationManager.GetSection(....)') - 在我的情況下工作! – 2010-07-02 06:06:34

回答

15

你真的不需要--WCF運行時會爲你做所有這些。

如果你真的必須 - 無論出於何種原因 - 你可以這樣做:

using System.Configuration; 
using System.ServiceModel.Configuration; 

ClientSection clientSettings = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection; 

string address = null; 

foreach(ChannelEndpointElement endpoint in clientSettings.Endpoints) 
{ 
    if(endpoint.Name == "XXX") 
    { 
     address = endpoint.Address.ToString(); 
     break; 
    } 
} 
+0

我們如何定義類ClientSection在代碼中使用?我不知道如何定義端點屬性。我們可以在那裏使用ArrayList嗎? – 2010-07-01 03:17:46

+0

@Nam Gi VU:ClientSection在System.ServiceModel.Configuration中定義,並且它有一個Endpoints集合。你在這裏沒有定義任何東西 - 你只是使用現有的框架類。 – 2010-07-01 06:25:00

+0

我已編輯我的問題。請看一看。 – 2010-07-01 10:17:40

0
var config = ConfigurationManager.OpenExeConfiguration("MyApp.exe.config"); 
var wcfSection = ServiceModelSectionGroup.GetSectionGroup(config); 
var clientSection = wcfSection.Client; 
foreach(ChannelEndpointElement endpointElement in clientSection.Endpoints) { 
    if(endpointElement.Name == "XXX") { 
     return endpointElement.Address; 
    } 
} 
3

可以使用ServiceModelSectionGroup(System.ServiceModel.Configuration)訪問配置:

var config = ConfigurationManager.GetSection("system.serviceModel") as ServiceModelSectionGroup; 
    foreach (ChannelEndpointElement endpoint in config.Client.Endpoints) 
    { 
     Uri address = endpoint.Address; 
     // Do something here 
    } 

希望有所幫助。