Are Static Class Instances Unique to a Request or a Server in ASP.NET

Are static class instances unique to a request or a server in ASP.NET?

Your static classes and static instance fields are shared between all requests to the application, and has the same lifetime as the application domain. Therefore, you should be careful when using static instances, since you might have synchronization issues and the like. Also bear in mind, that static instances will not be GC'ed before the application pool is recycled, and therefore everything that is referenced by the static instance, will not be GC'ed. This can lead to memory usage problems.

If you need an instance with the same lifetime as a request, I would suggest to use the HttpContext.Current.Items collection. This is by design meant to be a place to store stuff that you need througout the request. For nicer design and readability, you can use the Singleton pattern to help you manage these items. Simply create a Singleton class that stores its instance in HttpContext.Current.Items. (In my common library for ASP.NET, I have a generic SingletonRequest class for this purpose).

is there separate static class for each user in web Appllication in asp.net?

static classes in asp.net are shared across threads and there is no direct correlation between threads and users. One user may user multiple threads to handle client side requests or multiple users may use only a single thread to handle all of their requests. This is dependent on server configuration and load.

While static methods can be used in an asp.net web application, static members (fields and properties) should be avoided. Theses underlying variable / containers are shared and one thread will over-write the values of another thread and lead to very difficult debugging scenarios.

Marking a class static requires that all methods and members be static and should generally be avoided in asp.net. I have used static classes from time to time to expose instance based objects and values external to the class. There is also the example of extension methods which are fine in asp.net as they don't contains static fields.

When and who creates the instance of static class?

A static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.

The following list provides the main features of a static class:

  • Contains only static members.
  • Cannot be instantiated.
  • Is sealed.
  • Cannot contain Instance Constructors.

Static classes and security

Objects live in a so called AppPool in the IIS. As long as that is not recycled, objects with static lifetime will be available. As one cannot reliably know when recycling happens, having static variables is a bad idea either way. Using them to hold data between calls or assuming they will not hold data between calls is both equally dangerous.

That said, if your static class does not hold data and only consists of methods, that's perfectly fine.



Related Topics



Leave a reply



Submit