是否有可能在运行时改变一个ASP.NET的Web.Config值无需重新加载应用程序域? [英] Is it possible to change an ASP.NET Web.Config value at runtime without reloading the application domain?

查看:138
本文介绍了是否有可能在运行时改变一个ASP.NET的Web.Config值无需重新加载应用程序域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能改变在运行时的Web.Config数据库连接字符串值,而无需重新加载应用程序域?这样做的原因这是:

Is it possible to change a database connection string value in the Web.Config at runtime without reloading the application domain? The reason for doing this is that:


  1. 我建立它使用一个code实例和多个数据库实例的方法多租户应用程序,所以在web.config数据库连接字符串必须能够在运行时对不同的租户被改变用户。

  1. I am building a multi-tenanted application which uses the one code instance and multiple databases instances approach, so the database connection string in the web.config must be able to be changed at runtime for different tenant users.

ADO.NET总是injecst在web.config中的数据库连接字符串时,使用的是实体框架/ LINQ到SQL等数据集等。

ADO.NET always injecst the database connection string in the web.config when you are using entity framework/LINQ to SQL etc, dataset etc.

所以,我可能必须做一些奇怪的是这样的。希望它是非常明显的。非常感谢!

So I may have to do something weird like this. Hope it is clear enough. Thanks a lot!

推荐答案

是的,它是可能的。
试试这个

Yes it is possible. try this

System.Configuration.Configuration conf = WebConfigurationManager.OpenWebConfiguration(Server.MapPath);
conf.ConnectionStrings.ConnectionStrings["comp1"].ConnectionString = _connection_comp1;
conf.ConnectionStrings.ConnectionStrings["comp2"].ConnectionString = _connection_comp2;
conf.AppSettings.Settings["CompanyCode"].Value = _company_code;
conf.Save();

这篇关于是否有可能在运行时改变一个ASP.NET的Web.Config值无需重新加载应用程序域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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