如何以编程方式将sectionGroup添加到web.config中
背景
我想将以下内容插入到我的 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 thesection
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个简单的示例,将向 app.config 添加如下所示的部分:
A simple example that will add a section like the following to app.config: