2012-07-10 62 views
-3

我需要在我的Web.config文件中創建嵌套節,我找不到符合我的要求的任何示例。web.config中的嵌套節

<IPTests> 
    <Environment environment="DEV"> 
     <Machine machine="Web01"> 
     <SiteIP site="Sitecore" ip="10.10.2.191"> 
     </SiteIP> 
     </Machine> 
    </Environemnt> 
</IPTests> 

這是用於「健康檢查」爲不同的應用程序/網站。我需要檢查所有不同網站使用的資源。我已經使用DNS完成了這項工作,但是現在我需要在我們的不同服務器的不同環境中執行此操作,方法是使用IP地址訪問不同的服務器。

任何幫助將是偉大的!

這是我到目前爲止。

public class IPTests : ConfigurationSectionGroup 
    { 
     [ConfigurationProperty("codeEnvironment")] 
     public CodeEnvironmentSection CodeEnvironment 
     { 
      get { return (CodeEnvironmentSection)base.Sections["codeEnvironment"]; } 
     } 
    } 

    public class CodeEnvironmentSection : ConfigurationSection 
    { 
     [ConfigurationProperty("environemnt")] 
     public ValueElement To 
     { 
      get { return (ValueElement)base["environemnt"]; } 
     } 
    } 

    public class MachineSection : ConfigurationSection 
    { 
     [ConfigurationProperty("machine")] 
     public ValueElement To 
     { 
      get { return (ValueElement)base["machine"]; } 
     } 
    } 

    public class SiteIPSection : ConfigurationSection 
    { 
     [ConfigurationProperty("site")] 
     public ValueElement To 
     { 
      get { return (ValueElement)base["site"]; } 
     } 

     [ConfigurationProperty("ip")] 
     public ValueElement To 
     { 
      get { return (ValueElement)base["ip"]; } 
     } 
    } 

    public class ValueElement : ConfigurationElement 
    { 
     [ConfigurationProperty("value")] 
     public string Value 
     { 
      get { return (string)base["value"]; } 
      set { base["value"] = value; } 
     } 
    } 
+5

[你有什麼試過](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – 2012-07-10 18:09:51

+0

我一直在嘗試使用這個前面的例子:http://stackoverflow.com/questions/5027284/nested-configuration-section-app-config但是我不能讓它工作因爲我的多個巢 – Staleyr 2012-07-10 18:17:15

回答

0

它看起來像你試圖在你的web.config中存儲信息數據庫。如果這可能會改變,那麼你不應該把它存儲在這裏。

的問題是,任何改變的web.config會導致您的網站重新啓動,並重置用戶會話緩存等

如果您正致力於將自己的部分,那麼你可以通過創建做到這一點自定義ConfigurationSection類:

您可以在網絡上搜索更多信息這個,因爲有相當多的教程在那裏。

+0

我' m在不同的服務,數據庫,文件夾等上運行資源測試。我需要在不同的ips上運行這些測試(如果您願意的話,這是一項健康檢查測試)。並且信息不會經常改變,但如果這樣做需要輕鬆更改,那就是爲什麼它在web.config文件中。 – Staleyr 2012-07-10 18:25:54

+0

夠公平的,你可以把它放在一個容易支持嵌套元素的xml文件中 - 取決於你是如何使用ConfigurationSection類的煩惱:) – rtpHarry 2012-07-10 18:42:42

+0

非常非常惱火lol,但我只是一個新手實習生,是我的主管想要它的方式:/ – Staleyr 2012-07-10 18:48:40