What's the best way to manage a global variable in asp.net?

I currently have a NotInheritable class in App_Code that contains some variables that need to be accessed by the application, but I don't think this is a good way to manage global variables.

0


source to share


4 answers


Usually this type requires a Singleton . However, I would recommend to never code a single single yourself and use the Injection / IoC Dependency framework to handle the service lifecycle.



Another thing you have to remember with ASP.NET is that the ASP.NET process will automatically recycle itself every time and then, so you will need to save the changes to the permenant store (like filesystem or database)

+2


source


You must use the app settings. Just go to the application properties window, select the settings tab and add variables. Instead of using code, only use a typical class. For example, if I created a global variable named PortalName, I simply use:Settings.Default.PortalName



0


source


I believe the Application object is a built-in way of using global variables where you can store different values ​​in the object as needed.

0


source


As an alternative to application state, consider simply making your variable static. It will basically mean the same thing, with the added benefit of being strongly typed.

Personally, I would go with what David suggested and use a single core service that is managed by an IoC container.

0


source







All Articles