Web.config 和 App.config 的问题 [英] Problems with Web.config and App.config

查看:31
本文介绍了Web.config 和 App.config 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 通常我们在 Web.config 中存储 ConnectionStrings 和一些其他设置()或 App.config.
  • Normally we store ConnectionStrings and some other settings (<appSettings> <add key...) in the Web.config or App.config.
  • 使用工厂模式的Web应用程序直接注入读取数据供应商.

  • Web application using factory pattern with direct injection to read data providers.

web.config 中我有密钥这告诉我哪个 DLL(提供者)我将用来检索我的数据.

In the web.config I have the key that tells me which DLL (provider) will I use to retrieve my data.

我可以拥有多个提供者(每个 DLL 将成为 MS 的提供者SQL、MySQL 或从一些 SOA 服务).

I can have more than one provider (each DLL will be a provider for MS SQL, MySQL, or get the data from some SOA service).

每个 DLL 都有自己的名称(ID 和命名空间),并且需要拥有自己的名称配置(数据连接,服务网址等...),第一个想法是写然后在app.config.

Each DLL has his own name (ID and namespaces) and will need to have is own configurations (dataconnections, service urls, etc...) , the first idea is to write then in the app.config.

  • #1 - 网站正在运行(运行时)我需要更改数据提供程序,我该怎么做? 不知何故写入了默认值Web.config 将被更改.

  • #1 - The website is running (runtime) I need to change the Data Provider, how can I do this? Somehow the default value written in the Web.config will be changed.

  • 我的目标是能够拥有多个提供者(并且在运行时:添加/删除提供者和更改配置)——这让我想到了我的第二个问题:

.

#2 - 每个数据提供程序都有自定义配置,App.Config 文件不能用于 dll 程序集,只能用于可执行文件.这意味着我需要在我的 Web.Config 上写(我不喜欢这个选项,因为我再次在运行时更新我的​​ web.config).我该如何解决这个问题?

#2 - Each Data Provider has custom configurations and App.Config files do not work with dll assemblies, only executables. This means that I need to write then on my Web.Config (I do not like this option, because once again I am updating my web.config in runtime). how can I solve this?

  • 我试图避免编写自定义设置 XML 文件.我的理想解决方案是以某种方式为每个提供程序部署 DLLDLL.config.再次在运行时我可能需要更改此配置值.
  • I am trying to avoid to write a custom settings XML file. My ideal solution is to deploy somehow the DLL and DLL.config per each provider. And once again during runtime I may need to change this configuration values.

.

推荐答案

好吧,当我在等待帮助时,我开始着手工作并找到了一个好的解决方案(当然在我看来 :P).

Ok guys, while I was waiting for some help I put my hands to work and I was able to find a good solution (in my opinion of course :P).

让我与您分享:

因此,我有一个 Web 应用程序、一个控制台应用程序或其他类型的应用程序,以及许多类库,我需要存储在运行时会发生变化的信息(每个 Visual Studio 项目不同).

So, I have one web application, or one console application, or some other kind of application, and lots of class library, and I need to store informations (different per Visual Studio project) that will change during runtime.

将此信息存储在 Web.configApp.config 中并不是一个好主意,因为它会带来许多问题.

Storing this information inside the Web.config or App.config is not a good idea for the many problems it takes.

我认为的另一种方式是每个项目有一个 XML 配置文件.

The other way I see it is to have one XML config file per project.

每个应用程序将读取自己的 XML 并将其添加到带有 CacheDependency 的缓存中(将在 XML 配置文件更新时过期).这样我们就不需要一直读取配置,而且我们也知道什么时候配置发生了变化.

Each application will read his own XML and add it to the Cache with CacheDependency (will expire when the XML config file is updated). This way we will not need to read the configuration all the times, and we also know when the configuration is changed.

IMO 这是解决问题的最快和最简单的方法,无需使用第 3 方框架(无需花费时间学习/编程).

IMO THIS IS THE FASTEST AND EASIEST WAY TO SOLVE THE PROBLEM, no need to use 3rd party frameworks (neither the time it takes to learn/program it).

.

    protected void Page_Load(object sender, EventArgs e)
    {
        DBConfiguration cachConf;
        cachConf = Cache["cachConf"] as DBConfiguration;
        if (cachConf == null)
        {
            cachConf = new DBConfiguration();

            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/MyConf.xml");
            XmlNodeList xnl = doc.GetElementsByTagName("username");
            XmlElement xe = (XmlElement)xnl[0];
            cachConf.Username = xe.InnerText.ToString();
            xnl = doc.GetElementsByTagName("password");
            xe = (XmlElement)xnl[0];
            cachConf.Password = xe.InnerText.ToString();               

            Cache.Insert("cachConf", cachConf, 
                new System.Web.Caching.CacheDependency(
                    HttpContext.Current.Request.PhysicalApplicationPath + "MyConf.xml"),
                    DateTime.Now.AddMinutes(60), TimeSpan.Zero,
                    System.Web.Caching.CacheItemPriority.Default,
                    new System.Web.Caching.CacheItemRemovedCallback(
                        CacheItemRemovedCallBack));
        }
        LabelUsername.Text = cachConf.Username;
        LabelPassword.Text = cachConf.Password;            
    }

    private void CacheItemRemovedCallBack(string key, object value, CacheItemRemovedReason reason)
    {
        //Response.Write("Hello world"); 
    }

这篇关于Web.config 和 App.config 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆