Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Static Constructor question 1

Status
Not open for further replies.

rf222

Programmer
Jan 29, 2004
94
US
In Myclass I have 4 properties: Environment, DataStore, Session and User.

I call 2 static methods from constructor:

InitEnv -> initializes DataStore and Environment
InitSession -> initializes Session and User
Seems it should work fine, however when another user logins InitSession should be called again...

Question:

1) Is static constructor called once per user session or is for all users. When is it called, when appstarts or when session starts?

2) Is there a way to automaticly call it after user logins? Maybe by using delagates???
 
A static constructor is called the first time the class is used. Never again until the application restarts. So in ASP.NET, this would only be one time (or until the app domain gets recycled from a change to a dll or web.config or the worker process gettting restarted). So static data is effectively the same for all users.

If you want to store user-specific or session-specific data in memory between requests, store the data in the Session object. Only if the data is to be shared by all users should you use a static member.
 
What about the second question? How can I call static method automaticly each time user logins?
 
drogonwell just told you, the constructor is called ONCE, the first time the app starts. You cannot call it EACH time the user logs in.
[ static constructor is called the first time the class is used. Never again until the application restarts. So in ASP.NET, this would only be one time (or until the app domain gets recycled from a change to a dll or web.config or the worker process gettting restarted).
 
You can call a static METHOD whenever you want, such as when a user logs in - just do MyClass.MyStaticMethod(); -but I'm not sure about rigging up something to do it automatically. If you still need help, post a little more detail about the overall goal of what you're trying to do.
 
You can either call the static method wherever your login code resides, or do something like build an HttpModule and call the method on AuthorizeRequest.
 
Thanks - AuthorizeRequest! That is what I needed! How do I add my sub to AuthorizeRequest without modifying global.asax file? Delegates?
 
If you don't want to touch Global.asax, you'd create an HttpModule and configure your app to use it via a web.config setting. Global.asax might actually serve your needs, though.
 
I saw in C# there is syntax that you can add events outside of the actual event... Is it called delegates?
 
You can "subscribe" to "published" events using delegates, but this isn't particular to C#, it's part of the .NET Framework.

For instance you can observe the Load event of a page thusly:

Code:
Page.Load += new EventHandler(DelegateName);

//...

private void DelgateName( object sender, EventArgs e )
{
   //do stuff
}

The idea being that components can message each other without introducing a dependency. In other words, the Page has no knowledge of a subscriber at compile-time, but it can communicate actions to a subscriber assigned at run-time through a published event.

In your case, however, Global.asax or an HttpModule are probably the best places for the AuthorizeRequest code and with the HttpModule, you'd subscribe to the AuthorizeRequest event something like this:

Code:
public class MyModule : IHttpModule
{
   public virtual void Init(HttpApplication app)
   {
      app.AuthorizeRequest += new 
         EventHandler(myModule_AuthorizeRequest);
   }

   protected virtual void myModule_AuthorizeRequest(
     object sender, EventArgs e)
   {
      //do stuff
   }

   public virtual void Dispose() {}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top