What is the life span of a field in a static class

2019-06-02 05:50发布

问题:

I have a simple web service, in it i have a static class which has a static collection to remember alive tokens.

I just want to make sure that the token collection lives until the next iisreset or the application pool recycles.

And what is the difference between remembering states in Application bag and static class?

Thanks,

回答1:

Microsoft (says):

ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP.NET. It is recommended that you store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary.

So, use static variables. And do not forget use lock.



回答2:

Your static members won't scale to more than 1 server.