Solved

Global class constructor and Application_Start method

Posted on 2006-11-17
9
478 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

23 Experts available now in Live!

Get 1:1 Help Now