从物理文件读取连接字符串与应用程序对象(ASP.NET)的优点/缺点?

发布于 2024-08-28 18:52:47 字数 1527 浏览 5 评论 0原文

我的 ASP.NET 应用程序读取 xml 文件来确定它当前所处的环境(例如本地、开发、生产)。

它每次打开与数据库的连接时都会检查此文件,以便知道要从应用程序设置中获取哪个连接字符串。

我正在进入一个开发阶段,效率成为人们关注的焦点。我认为每次我希望访问数据库(非常频繁)时都必须读取物理磁盘上的文件不是一个好主意。

我正在考虑将连接字符串存储在 Application["ConnectionString"] 中。所以代码是

public static string GetConnectionString
        {
            if (Application["ConnectionString"] == null)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ServerEnvironment.xml");
                XmlElement xe = (XmlElement) xnl[0];

                switch (xe.InnerText.ToString().ToLower())
                {
                    case "local":
                        connString = Settings.Default.ConnectionStringLocal;
                        break;

                    case "development":
                        connString = Settings.Default.ConnectionStringDevelopment;
                        break;

                    case "production":
                        connString = Settings.Default.ConnectionStringProduction;
                        break;

                    default:
                        throw new Exception("no connection string defined");
                }
                Application["ConnectionString"] = connString; 
            }
            return Application["ConnectionString"].ToString();
        }

我没有设计应用程序,所以我认为每次读取 xml 文件肯定是有原因的(在应用程序运行时更改设置?)我对这里的内部工作原理知之甚少。有什么优点和缺点?您认为通过实现上述功能我会看到一点性能提升吗?

谢谢

my ASP.NET application reads an xml file to determine which environment it's currently in (e.g. local, development, production).

It checks this file every single time it opens a connection to the database, in order to know which connection string to grab from the Application Settings.

I'm entering a phase of development where efficiency is becoming a concern. I don't think it's a good idea to have to read a file on a physical disk ever single time I wish to access the database (very often).

I was considering storing the connection string in Application["ConnectionString"]. So the code would be

public static string GetConnectionString
        {
            if (Application["ConnectionString"] == null)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ServerEnvironment.xml");
                XmlElement xe = (XmlElement) xnl[0];

                switch (xe.InnerText.ToString().ToLower())
                {
                    case "local":
                        connString = Settings.Default.ConnectionStringLocal;
                        break;

                    case "development":
                        connString = Settings.Default.ConnectionStringDevelopment;
                        break;

                    case "production":
                        connString = Settings.Default.ConnectionStringProduction;
                        break;

                    default:
                        throw new Exception("no connection string defined");
                }
                Application["ConnectionString"] = connString; 
            }
            return Application["ConnectionString"].ToString();
        }

I didn't design the application so I figure there must have been a reason for reading the xml file every time (to change settings while the application runs?) I have very little concept of the inner workings here. What are the pros and cons? Do you think I'd see a small performance gain by implementing the function above?

THANKS

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

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

发布评论

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

评论(2

浅黛梨妆こ 2024-09-04 18:52:47

哇。把它扔进垃圾桶。

所有 .config 文件(web 或 app.config)都有一个专门用于连接字符串的部分,可以使用 ConfigurationManager.ConnectionStrings 属性。

对于本地和测试环境,我只需设置三个连接字符串,如下所示:

<connectionStrings>
    <add name="default.local" connectionString="etc ..>
    <add name="default.test" connectionString="etc ..>
    <add name="default" connectionString="etc ..>
</connectionStrings>

根据配置文件中的另一个设置确定要获取的连接。一个小实用程序函数利用此信息来选择正确的连接字符串。

这样,除了环境设置之外,配置文件在不同的部署中保持相同。

编辑:忘记提及这些值缓存在内存中,并且应用程序在更改时重新启动(可能是问题,可能有用)通过 ConfigurationManager 类直接访问 .config 文件,性能绝对很好。

Wow. Throw that in the bin.

All .config files (web or app.config) have a section dedicated to connection strings and these can be read using the ConfigurationManager.ConnectionStrings property.

For local and test environments, I simply set up three connection strings as follows

<connectionStrings>
    <add name="default.local" connectionString="etc ..>
    <add name="default.test" connectionString="etc ..>
    <add name="default" connectionString="etc ..>
</connectionStrings>

Determine which connection stirng to fetch based on another setting in the config file. A small utility function utilises this information to pick the correct connection string.

In this way, the config file stays the same across the different deployments apart from the environment setting.

EDIT: forgot to mention these values are cached in memory and the app restarts when they are changed (may be a problem, may be useful) Performance is absolutely fine going directly to the .config file through the ConfigurationManager class.

勿忘初心 2024-09-04 18:52:47

我完全同意詹姆斯·韦斯特盖特的观点。如果您使用web.config,则不必关心这个问题。

如果您因任何原因无法使用 web.config,我会说您的方法是正确的。将值存储在应用程序存储上,您可以避免从磁盘一遍又一遍地读取连接字符串。

如果需要更改连接字符串,则必须重新启动应用程序。连接字符串不应该每天都改变,所以这不应该是一个问题。

顺便说一句:我不太喜欢询问你处于哪种环境的逻辑。这应该完全没有必要吧...

I totally agree with James Westgate. If you use the web.config, you don't have to take care about this issue.

In case that you can't use the web.config for any reason I would say that your approach is correct. Storing the value on the Application storage you're avoiding to read the connections string over and over again from disk.

If you need to change the connection string, you would have to restart the application. A connection string is not supposed to change everyday so this shouldn't be a problem.

BTW: I don't like very much the logic asking in which environment you are. That shouldn't be necessary at all...

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