如何使用IConfigurationSectionHandler进行全局配置?

发布于 2024-09-18 18:05:29 字数 216 浏览 4 评论 0原文

我最近开始使用 IConfigurationSectionHandler 作为 BL DLL 的自定义配置部分。我在 web.config 文件中使用它来将设置值传递给 BL DLL。

虽然它可以完美读取本地 web.config,但问题是读取包含共享设置的全局配置文件(根 web.config)。

我如何使用 IConfigurationSectionHandler 来做到这一点?

I recently started using IConfigurationSectionHandler as a custom configuration section for my BL DLL. I'm using it inside web.config files to pass settings values to the BL DLL.

While it reads the local web.config perfectly, the problem is reading a global configuration file (root web.config) that consists of shared settings.

How can I manage to do that using IConfigurationSectionHandler?

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

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

发布评论

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

评论(3

为人所爱 2024-09-25 18:05:29

为了正确回答您的问题,我们可能需要从您的自定义部分处理程序中查看一些代码。

然而,我立即想到的一点是,您可能没有正确使用传递到处理程序中的 parent 参数。

为了让我们的术语在同一页上,我将引用“Create”方法,它是 IConfigurationSectionHandler.Create 的实现,以及一个 配置对象,它是从“Create”方法返回的对象。

非常简单地说,您的节处理程序 Create 方法应该在 web.config 文件层次结构中每次出现自定义节时调用。每次调用时,从 Create 返回的先前配置对象都会作为 parent 参数传递到下一个调用中,当然,第一次调用将具有 null< /code>parent 表示您需要创建这个初始“配置对象”,后续调用不应创建新的配置对象,而应添加或修改作为父级传入的配置对象。

最终结果是,当您从文件中读取配置时,您会收到一个“配置对象”,其中包含所有级别的设置总和。

注意:您确实应该使用 ConfigurationSection,因为 IConfigurationSectionHandler 自 Framework 2.0 起已被弃用。这是使用此类的链接。

http://msdn.microsoft.com/en-us/library/2tw134k3。 ASPX

To properly answer your question we would probably need to see some code from you custom section handler.

However, one point that immediately springs to mind, is that you might not be correctly using the parent argument that is being passed into your handler.

Just to get our terminoligy on the same page, I will refer to the 'Create' method which is your implementation of the IConfigurationSectionHandler.Create, and a configuration object which is the object that you return from the 'Create' method.

Very simplistically, your section handlers Create method should be invoked for each occurance of your custom section in the hierarchy of web.config files. With each invocation, previous configuration object that you returned from Create is passed into the next call as the parent argument, of course the first call will have a null parent which indicates that you need to create this initial 'configuration object', subsequent calls should not create a new configuration object but add to or modify the one passed in as the parent.

The end result is that when you read the configuration from the file you receive a 'configruation object' that contains the sum of the settings from all levels.

NB: You should really be using ConfigurationSection, since IConfigurationSectionHandler has been deprecated since Framework 2.0. Here is a link to using this class.

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

歌入人心 2024-09-25 18:05:29

我更喜欢继承 ConfigurationSection 而不是实现 IConfigurationSectionHandler,因为您不必手动处理 XML。

看看这些链接。每个解释了上述其中一项:

http://support.microsoft.com/kb/309045

< a href="http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

I would prefer inheriting ConfigurationSection over implementing IConfigurationSectionHandler since you do not have to manually deal with the XML.

Have a look at these links. Each explains one of above:

http://support.microsoft.com/kb/309045

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

两个我 2024-09-25 18:05:29

这是在 web.config 中使用我们自己的配置类的示例。
假设我们有一个类要在 web.config 中初始化并在我们的代码中使用。

这是我们的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace MyProject.MyConfigSection
{
    public class MyConfig:System.Configuration.IConfigurationSectionHandler
    {
        public int MyNum1 { get; set; }
        public int MyNum2 { get; set; }
        public int MyNum3 { get; set; }

    public MyConfig()
    {
        MyNum1 = 0;
        MyNum2 = 0;
        MyNum3 = 0;
    }

//implement interface member
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
    {
        try
        {
            MyConfig options = new MyConfig();
            if (section == null) return options;

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.Name == "MyNum1")
                    options.MyNum1 = int.Parse(node.InnerText);
                else if (node.Name == "MyNum2")
                    options.MyNum2 = int.Parse(node.InnerText);
                else if (node.Name == "MyNum3")
                    options.MyNum3 = int.Parse(node.InnerText);
            }
            return options;
        }
        catch (Exception ex)
        {
            throw new System.Configuration.ConfigurationException(
             "Error loading startup default options", ex, section);
        }

    }
}

}

现在我们在 web.config 中使用名称来声明它。

<configuration>
    <configSections>
        <section name="MYTESTCONFIGSECTION" type="MyProject.MyConfigSection.MyConfig" />
.... //other sections
.... //other sections
    </configSections>

现在,在 web.config 本身中,我们将其添加到配置标记之间的任意位置:

<MYTESTCONFIGSECTION>
    <MyNum1>111</MyNum1>
    <MyNum2>222</MyNum2>
    <MyNum3>333</MyNum3>
</MYTESTCONFIGSECTION>
</configuration>

现在我们可以通过如下代码访问此部分:

var myconfig = System.Web.Configuration.WebConfigurationManager.GetSection("MYTESTCONFIGSECTION") as MyConfigSection.MyConfig;
    myconfig.MyNum1;
    myconfig.MyNum2;
    myconfig.MyNum3;

希望这对某人有帮助。

This is an example of using our own configuration class in web.config.
Lets say we have a class which is to be initialized in web.config and used in our code.

Here is our class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace MyProject.MyConfigSection
{
    public class MyConfig:System.Configuration.IConfigurationSectionHandler
    {
        public int MyNum1 { get; set; }
        public int MyNum2 { get; set; }
        public int MyNum3 { get; set; }

    public MyConfig()
    {
        MyNum1 = 0;
        MyNum2 = 0;
        MyNum3 = 0;
    }

//implement interface member
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
    {
        try
        {
            MyConfig options = new MyConfig();
            if (section == null) return options;

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.Name == "MyNum1")
                    options.MyNum1 = int.Parse(node.InnerText);
                else if (node.Name == "MyNum2")
                    options.MyNum2 = int.Parse(node.InnerText);
                else if (node.Name == "MyNum3")
                    options.MyNum3 = int.Parse(node.InnerText);
            }
            return options;
        }
        catch (Exception ex)
        {
            throw new System.Configuration.ConfigurationException(
             "Error loading startup default options", ex, section);
        }

    }
}

}

Now we declare this with a name in web.config.

<configuration>
    <configSections>
        <section name="MYTESTCONFIGSECTION" type="MyProject.MyConfigSection.MyConfig" />
.... //other sections
.... //other sections
    </configSections>

Now in web.config itself, we add this anywhere between configuration tags:

<MYTESTCONFIGSECTION>
    <MyNum1>111</MyNum1>
    <MyNum2>222</MyNum2>
    <MyNum3>333</MyNum3>
</MYTESTCONFIGSECTION>
</configuration>

Now we can access this section from code like this:

var myconfig = System.Web.Configuration.WebConfigurationManager.GetSection("MYTESTCONFIGSECTION") as MyConfigSection.MyConfig;
    myconfig.MyNum1;
    myconfig.MyNum2;
    myconfig.MyNum3;

Hope this helps someone.

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