How will Static behave here in asp.net?
My question is: Will this only wor开发者_开发问答k for a specific user session or for overall application ?
Edited: Based on some answers I got another question here.
let say i am using the Static method as Authenticate(User objUser)
. intention is to call this method when user clicked log in button. where on login button I am let say prepare the User object with certain parameters required for login, then passing to this method. what would be the impact there? let say I have single server for now (no server farm or garden). then there will be the single application level method to authenticate the user, right? and say 10000 user are going to logging in to this site/application then how authenticate() method comes in behaves ? will CLR automatically manage threading there ?
Static variables are at the application level, so it's shared across sessions.
So your situation:
If the user logs in successful I set IsLoggedInSuccessfull as a static boolean variable in common class.
Would mean that now, for every user logging into the web application, if you check the variable IsLoggedInSuccessfull
, the value would be true. So you probably want to keep this variable as a session variable, not a static variable.
Also keep in mind that if you have a web farm, each server has an instance of your web application running on it - meaning that on each application instance, the static variables can potentially have different values (if you are not careful). Same principle for web gardens...
Regarding your second question:
CLR does not manage thread safety - you need to take care of it yourself. That being said, if the static method does not use any global static variables in itself, and if the User
instance you pass into the static method is unique, you should be safe. If the User
instance is not unique, make sure to put thread safety code in the appropriate places in the User
class.
If you're making the change to a static class it will be for the specific instance of the overall application.
If you are running multiple instances that are load balanced, it will only be set in one of them.
A static class will maintain the value.
But, using Session
instead is much better because it is more intuitive in a web application and is a widely used practice.
Here is a good article on the subject:
Managing Your Static Data in ASP.NET
A static variable will be shared across application instance, meaning that it is the same variable for every request (user), while you are running single instance app.
For user specific information use session
object.
精彩评论