静态对象|环境独立 Web.config 的自定义 ConfigurationManager

发布于 2024-12-07 14:24:38 字数 2425 浏览 0 评论 0原文

我正在开发一个典型的 .net Web 应用程序,该应用程序首先部署在测试环境 - ENV-T 然后在完成测试后将其移至 Production ENV - P。 我们的托管提供商不会直接在 Prod 上部署任何内容,而是首先在 Test 上部署,然后将其同步到 PROD。

而且这两个环境Env-T和Env-P是完全不同的。因此 Web.Config 设置也不同。

因此,要在 ENV-P 上部署任何内容,我们必须根据 Env-P 更改 ENV-T 的 web.config。然后要求托管供应商同步应用程序,然后我们必须恢复。

为了解决这个问题,我想到了使 Web.config 环境独立。因此,我编写了一个自定义 ConfiguraitonManager 类,它将识别应用程序当前工作的环境并相应地加载值。

高级方法:我们将有两个 Web.config,一个位于 ENV-T 文件夹中,另一个位于 ENV-P 文件夹中。根据机器名称,我们将识别环境。如果是 ENV-T,则从 ENV-T 文件夹加载 Web.Config 值;如果是 ENV-P,则从 ENV-P 文件夹加载值。

这是我的课程:

public static class ConfigurationManager
{
    static Configuration _config;
    static NameValueCollection nameValueColl;
    public static NameValueCollection AppSettings
    {
        get
        {

if (nameValueColl == null)
            {
                if (_config == null)
                    _config = LoadConfig();
                nameValueColl = new NameValueCollection();
                foreach (KeyValueConfigurationElement configElement in _config.AppSettings.Settings)
                {
                    nameValueColl.Add(configElement.Key, configElement.Value);

                }

            }
            return nameValueColl;
        }
    }

    private static Configuration LoadConfig()
    {
        string basePath = string.Empty;


        if (System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath != null)
        {
            basePath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
        }

        string machineName = System.Environment.MachineName;

        if (machineName.Equals("MachineA"))
        {
            _config = WebConfigurationManager.OpenWebConfiguration(basePath + "Test");
        }
        else if (machineName.ToLower().Equals("MachineB"))
        {
            _config = WebConfigurationManager.OpenWebConfiguration(basePath + "Prod");
        }


        return _config;
    }

}

现在我所要做的就是写入

ConfigurationManager.AppSettings["KEY1"]

读取应用程序设置键“KEY1”,然后我获得该特定环境的正确值。

问题: 我有一把钥匙

由于某种原因,经过几次点击(一两天内)后,我得到的 NoOfRecords 键值是 50,50,50,50,50,而不是 50。 可能的原因: 如果我添加一个键,值“NoOfRecords”,“50”到已经有“NoOfRecords”,“50”的NameValueCollection对象,它将将该值存储为 “记录数”,“50,50”。

但是,这又是如何可能的,因为就在这个代码块之前,我正在创建一个新对象,并且我对此 nameValueColl 变量进行了空检查。

I am working on a typical .net web app which is first deployed at test environment - ENV- T
and then after complete testing it is moved to Production ENV - P.
Our hosting provider will not deploy anything directly on Prod instead it is first deployed on Test and then they sync it to PROD.

Also these two environment Env-T and Env-P are totally different. So the Web.Config settings are different too.

So to deploy anything on ENV-P we have to change the web.config of ENV-T as per Env-P. And then ask the hosting vendor to sync the application and then we have to revert.

To solve this issue i thought of making the Web.config environment independent. So i wrote a custom ConfiguraitonManager class which will identify the current environment in which the application is working in and will load values accordingly.

High level Approach : We will have two Web.config one in ENV-T folder and another in ENV-P folder. Depending upon the machine name we will identify the environment. If it is ENV-T load the Web.Config values from ENV-T folder and if it is ENV-P load the values from ENV-P folder.

Here is the my class :

public static class ConfigurationManager
{
    static Configuration _config;
    static NameValueCollection nameValueColl;
    public static NameValueCollection AppSettings
    {
        get
        {

if (nameValueColl == null)
            {
                if (_config == null)
                    _config = LoadConfig();
                nameValueColl = new NameValueCollection();
                foreach (KeyValueConfigurationElement configElement in _config.AppSettings.Settings)
                {
                    nameValueColl.Add(configElement.Key, configElement.Value);

                }

            }
            return nameValueColl;
        }
    }

    private static Configuration LoadConfig()
    {
        string basePath = string.Empty;


        if (System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath != null)
        {
            basePath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
        }

        string machineName = System.Environment.MachineName;

        if (machineName.Equals("MachineA"))
        {
            _config = WebConfigurationManager.OpenWebConfiguration(basePath + "Test");
        }
        else if (machineName.ToLower().Equals("MachineB"))
        {
            _config = WebConfigurationManager.OpenWebConfiguration(basePath + "Prod");
        }


        return _config;
    }

}

Now all i have to do is write

ConfigurationManager.AppSettings["KEY1"]

to read the Appsetting key "KEY1" and i get the correct value for that particular environment.

Question :
I have a key
<add key="NoOfRecords" value="50"/> .

For some reason after several hits ( in a day or two) the value i get for NoOfRecords key is 50,50,50,50,50 instead of 50.
Possible reason :
If i add a key,value say "NoOfRecords","50" to a NameValueCollection object which Already has "NoOfRecords","50" it will store the value as
"NoOfRecords","50,50".

But again how this is possible cause just before this code block i am creating a new object and also i have a null check for this nameValueColl variable.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文