Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Global class constructor and Application_Start method

Posted on 2006-11-17
9
487 Views
Last Modified: 2011-10-03
How do these two functions of the Global class in Global.asax differ?

1)constructor - public Global()
2)Application_Start - protected void Application_Start(Object sender, EventArgs e)

Please help. Thank you.
0
Comment
Question by:engg
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17978764
There is a specific chain of events in the lifetime of an application.  

The constructor is called when the class is instantiated through code.  The Application_Start is an event handler when the application is started.

Reference:

ASP.NET Application Life Cycle Overview
http://msdn2.microsoft.com/en-gb/library/ms178473.aspx

<Quote>The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.</Quote>

Bob
0
 

Author Comment

by:engg
ID: 17992440
Thanks Bob. I will read this article and I will get back soon.
0
 

Author Comment

by:engg
ID: 18057265
Quote from the above MSDN article:
<Quote>The first time an ASP.NET page or process is requested in an application, a new instance of HttpApplication is created. However, to maximize performance, HttpApplication instances might be reused for multiple requests..</Quote>

So suppose the application has been built and it receives 100 requests during the day.

Then Application_Start() method will be called exactly ONCE during that day.
But constructor Global() will be called 'n' number of times during that day where  1<= n <= 100.

Please correct me if I am wrong.


0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 18076375
No, I don't believe that the Global class is instantiated for each request, so the constructor will only be called when the class is created.  There really isn't much difference between Application_Start and the Global constructor, other than when in the application's life cycle they are called.

Bob
0
 

Author Comment

by:engg
ID: 18077020
Thanks Bob.
So what's the best place to add the code which we want to execute on every web request? - the code which operates on global data that is accessible throughout the application.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 18077115
Now we're gettin' down to it :)

If you need to run code on every request, then add it to the Application.BeginRequest event handler:

  protected void Application_BeginRequest(Object sender, EventArgs e)
  {
  }

Bob

0
 

Author Comment

by:engg
ID: 18078107
yeah :).
It works for me. Thanks Bob.
One more thing - I am reading about HttpApplication.Init and HttpApplication.Dispose. Could you give an example when could it be useful to override these methods?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 500 total points
ID: 18078191
Now there's a subject that hasn't come up before.  I don't know when it would be useful to use the HttpApplication.Init and HttpApplication.Dispose methods.

There are very few resources for those methods.  I could imagine that if you want to override the default event chain or HTTP modules, then you would think about using it.  Otherwise, it has never been a topic of discussion in all my time with ASP.NET.

Bob
0
 

Author Comment

by:engg
ID: 18078252
Thank you very much, Bob
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question