Solved

Global class constructor and Application_Start method

Posted on 2006-11-17
9
472 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now