我有一个使用一些配置设置的扩展方法.我已经宣布这些为static.
public static class Extensions
{
static string _mailServer = ConfigurationManager.AppSettings["MailServer"];
// ... etc
public static void SendEmailConfirmation(this IOrder order) { }
}
Run Code Online (Sandbox Code Playgroud)
我只是想检查一下这是否符合我的意图,因为我并不是100%肯定.我的想法是,我不想继续阅读这些值,我希望它们能够被读取一次,并在Web应用程序的生命周期中进行缓存.这会发生什么?谢谢
Mic*_*ren 16
(根据KeithS的说明更新,直到首次使用时才读取)
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.
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.