用的Web.config和App.config中存在的问题 [英] Problems with Web.config and App.config

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

问题描述


  • 通常我们存储的ConnectionStrings 和其他一些设置(<的appSettings><添加关键... )在的Web.config 的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和命名空间),并需要有自己的是
配置(dataconnections,
服务网址等),在第一
想法是写然后在
的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文件。我的理想的解决方案是不知何故 DLL DLL.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.config 的App.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这是解决问题的,无需使用第三方框架(它需要学习既没有时间/程序的话)最快和最简单的方法。

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天全站免登陆