.net - ASP.NET 应用程序变量与从 ConfigurationSettings.AppSettings 读取

标签 .net asp.net performance global-asax

Application Variable Vs Web.Config Variable 上发布了类似的问题,但我的略有不同,我不相信那里的答案。

因此,如果我在 global.asax 中定义一个应用程序变量,如下所示:

public class Global : System.Web.HttpApplication
{    
   public static readonly string X =  ConfigurationSettings.AppSettings["XsltExternal"];
   // rest of the code here
}

这个读操作不应该吗

string Y = Global.X;

比更快

string Y = ConfigurationSettings.AppSettings["XsltExternal"];

因为避免哈希表查找(假设这就是 ASP.Net 存储 web.config 设置的方式)?我的应用程序使用大量的配置设置并在整个页面周期中对其进行检查,因此我希望能够利用可以节省的每一毫秒。

有人有什么想法吗?

PS:我最初的简单测试页 ANTS 分析器测试显示读取时间从 0.017 毫秒下降到 0.002 毫秒。

最佳答案

我会说是的,它更快,但我会尽力保持 Global 类尽可能干净。在我的实现中,我通常将所有配置项放在带有静态构造函数的单独类中,例如:

public class Constants
{
    public static string PayPalSeller;
    public static string PayPalUrl;
    public static string PayPalPDTKey;
    public static string RpxTokenUrl;
    public static string VirtualAppFolder;
    static Constants()
    { 
        PayPalSeller = ConfigurationManager.AppSettings["PayPalSeller"];
        PayPalUrl = ConfigurationManager.AppSettings["PayPalUrl"];
        PayPalPDTKey = ConfigurationManager.AppSettings["PayPalPDTKey"];
        RpxTokenUrl = ConfigurationManager.AppSettings["RpxTokenUrl"];
    }
 }

要使用它,你当然会去:

Constants.PayPalSeller

希望这有帮助, -科沃

关于.net - ASP.NET 应用程序变量与从 ConfigurationSettings.AppSettings 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837617/

相关文章:

c# - CSV 换行符

c# - 从 javascript 渲染的页面获取 HTML 内容

asp.net - 是否可以将 Entity Framework 代码优先与 Web 窗体 (Asp.NET) 结合使用?

python - 如何通过一些技巧更快地完成这项任务?

ms-access - 有效利用 MS Access 中的计算

c# - 匿名实例化语法——好还是坏?

.net - 使用 DataContractSerializer 和 XmlSerializer 进行 XML 序列化

c# - 从azure openai sdk api返回流的正确方法

asp.net - aspnet_regiis 不存在

c# - Visual Studio 2015 不是语法高亮 Razor ,也不是 Intellisense