Solved

Global class constructor and Application_Start method

Posted on 2006-11-17
9
507 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

729 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