如何以编程方式将sectionGroup添加到web.config中

发布于 2024-10-10 20:59:00 字数 3023 浏览 3 评论 0原文

背景

我想将以下内容插入到我的 web.config 中

  <sectionGroup name="elmah">
    <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
    <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
    <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
  </sectionGroup>

(猜测我想要实现的目标没有奖品!)但我完全困惑了。 MSDN 上的文档建议,如果我想向组中添加一个 ConfurationSection,我需要创建 ConfurationSection 的子类。我自己写了一个小 Windows 应用程序来帮助我解决这个问题,但我并没有走得太远!这是相关的代码 - 它尝试仅添加“安全”部分。

private void AddElmahSectionGroup()
{
    string exePath = Path.Combine(Environment.CurrentDirectory, "NameOfExe.exe");
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(exePath);

    ConfigurationSectionGroup elmahGroup = configuration.GetSectionGroup(elmahSectionGroupName);
    if (elmahGroup != null)
    {
        Console.WriteLine("sectionGroup with name {0} already in web.config", elmahSectionGroupName);
        return;
    }
    elmahGroup = new ConfigurationSectionGroup();
    configuration.SectionGroups.Add(elmahSectionGroupName, elmahGroup);

    var securitySection = new Section { Name = "security", RequirePermission = false, Type = "Elmah.SecuritySectionHandler, Elmah" };
    elmahGroup.Sections.Add("security", securitySection);

    configuration.Save();
}

public class Section : ConfigurationSection
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name { get { return (String)this["name"]; } set { this["name"] = value; } }

    [ConfigurationProperty("requirePermission", IsRequired = true)]
    public bool RequirePermission { get { return (bool)this["requirePermission"]; } set { this["requirePermission"] = value; } }

    [ConfigurationProperty("type", IsRequired = true)]
    public string Type { get { return (string)this["type"]; } set { this["type"] = value; } }
}

这是最终的配置文件

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="elmah" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
            <section name="security" type="ConfigEditing.Form1+ElmahLogic+Section, ConfigEditing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </sectionGroup>
    </configSections>
    <elmah>
        <security name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
    </elmah>
</configuration>

,它完全扭曲了我的瓜: -

  • 首先, section 元素的 type 是我的类的类型(从 ConfigurationSection 派生),
  • 其次是将sectionGroup定义添加到组中,我(同时)将组添加到配置中。

我对这些发现并不感到惊讶,因为我真的不理解 API,但我只是没有找到任何关于我想做的事情的像样的文档。这里的任何人都可以提供帮助吗 - 即使它只是向我指出一个 MSDN 示例,该示例是一个实际的完整工作示例。

Background

I want to insert the following into my web.config

  <sectionGroup name="elmah">
    <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
    <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
    <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
  </sectionGroup>

(no prizes for guessing what I am trying to achieve!) but I am getting thoroughly confused. The docs on MSDN suggest that I need to create a subclass of ConfurationSection if I want to add one to a group. I wrote myself a little windows application to help me figure it out but I did not get very far! Here is the pertinent code - which tries to add the just the "security" section.

private void AddElmahSectionGroup()
{
    string exePath = Path.Combine(Environment.CurrentDirectory, "NameOfExe.exe");
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(exePath);

    ConfigurationSectionGroup elmahGroup = configuration.GetSectionGroup(elmahSectionGroupName);
    if (elmahGroup != null)
    {
        Console.WriteLine("sectionGroup with name {0} already in web.config", elmahSectionGroupName);
        return;
    }
    elmahGroup = new ConfigurationSectionGroup();
    configuration.SectionGroups.Add(elmahSectionGroupName, elmahGroup);

    var securitySection = new Section { Name = "security", RequirePermission = false, Type = "Elmah.SecuritySectionHandler, Elmah" };
    elmahGroup.Sections.Add("security", securitySection);

    configuration.Save();
}

public class Section : ConfigurationSection
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name { get { return (String)this["name"]; } set { this["name"] = value; } }

    [ConfigurationProperty("requirePermission", IsRequired = true)]
    public bool RequirePermission { get { return (bool)this["requirePermission"]; } set { this["requirePermission"] = value; } }

    [ConfigurationProperty("type", IsRequired = true)]
    public string Type { get { return (string)this["type"]; } set { this["type"] = value; } }
}

And here is the resultant configuration file

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="elmah" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
            <section name="security" type="ConfigEditing.Form1+ElmahLogic+Section, ConfigEditing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </sectionGroup>
    </configSections>
    <elmah>
        <security name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
    </elmah>
</configuration>

Which completely twisted my melon: -

  • first of all the type of the section element is the type of my class (derived from ConfigurationSection)
  • second by adding a sectionGroup definition into the group, I (at the same time) adding the group into the config.

I am not really surprised by these findings because I really don't understand the API but I just have not found any decent docs about what I want to do. Can anyone here help - even if it just pointing me at an MSDN example that is an actual full working example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2024-10-17 20:59:00

一个简单的示例,将向 app.config 添加如下所示的部分:

//<configSections>
//  <sectionGroup name="elmah" type="Overflow.CustomConfigurationSectionGroup, Overflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
//  </sectionGroup>
//</configSections>

namespace Overflow
{
    public class CustomSecuritySection : ConfigurationSection
    {
    }

    public class CustomConfigurationSectionGroup : ConfigurationSectionGroup
    {
        public CustomConfigurationSectionGroup()
        {
            Security = new CustomSecuritySection();
        }

        [ConfigurationProperty("security")] 
        public CustomSecuritySection Security { get; private set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var config = ConfigurationManager.OpenExeConfiguration(Path.Combine(Application.StartupPath, Application.ProductName + ".exe"));

            config.SectionGroups.Add("elmah", new CustomConfigurationSectionGroup());

            config.Save(ConfigurationSaveMode.Modified);

        }
    }
}

A simple example that will add a section like the following to app.config:

//<configSections>
//  <sectionGroup name="elmah" type="Overflow.CustomConfigurationSectionGroup, Overflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
//  </sectionGroup>
//</configSections>

namespace Overflow
{
    public class CustomSecuritySection : ConfigurationSection
    {
    }

    public class CustomConfigurationSectionGroup : ConfigurationSectionGroup
    {
        public CustomConfigurationSectionGroup()
        {
            Security = new CustomSecuritySection();
        }

        [ConfigurationProperty("security")] 
        public CustomSecuritySection Security { get; private set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var config = ConfigurationManager.OpenExeConfiguration(Path.Combine(Application.StartupPath, Application.ProductName + ".exe"));

            config.SectionGroups.Add("elmah", new CustomConfigurationSectionGroup());

            config.Save(ConfigurationSaveMode.Modified);

        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文