在.NET静态变量的生存期 [英] Lifetime of static variables in .NET

查看:124
本文介绍了在.NET静态变量的生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一些配置设置的扩展方法。我已经声明它们作为静态

I have an extension method which uses some configuration settings. I've declared these as static.

public static class Extensions
{
    static string _mailServer = ConfigurationManager.AppSettings["MailServer"];
    // ... etc    

    public static void SendEmailConfirmation(this IOrder order) { }
}

我只是想确认这是做什么的,我打算为我不是100%肯定。我们的想法是,我不希望有继续读这些价值观,我希望他们可以一次读取和缓存的Web应用程序的生命周期。这是会发生什么?谢谢

I just wanted to check that this is doing what I intend as I'm not 100% sure. The idea is that I don't want to have to keep reading these values, I'd like them to be read once and cached for the lifetime of the web application. Is this what will happen? Thanks

推荐答案

(更新KeithS的澄清,他们没有看过,直到第一次使用)

(updated with KeithS's clarification that they aren't read until first used)

他们将要读取的第一次使用,并一直保持到应用程序域停止或回收,这是你想要的大概是什么。

They will be read the first time they are used, and then retained until the AppDomain is stopped or recycled, which is probably what you want.

也就是说,ASP.NET应用程序的AppDomain中运行。这是他们如何居住,并提供给多个请求,而无需启动为每个单独的请求。您可以配置他们住多久以及何时回收等静态变量存亡与应用程序,因此,只要应用程序是驻留在应用程序域才能生存。

That is, ASP.NET apps run inside an AppDomain. This is how they are resident and available to multiple requests without having to startup for each individual request. You can configure how long they live and when they recycle, etc. Static variables live and die with the app and thus will survive as long as the app is resident in the app domain.

这篇关于在.NET静态变量的生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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