Application_Start global.asax being called frequently

I have a web application in c# and in the Application_Start method in the global.asax i have some code that sends me an email.

on almost every action i receive an email. this means that the method Application_Start is being called on every page.

Why does this happen and what are some possible consequences.

thanks.

salalimosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Very strange, Application_Start should only fire once when your web applications calls the first page. Thus, the Application_Start event is fired the first time when an application starts.
Check:
msdn.microsoft.com/en-us/library/ms178473.aspx

Can you post some code snippet of your global.asax? (Take any business related code out).
Is your web application in a separate app pool in IIS?
What version of OS, IIS, VS, .net are you using?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Miguel OzSoftware EngineerCommented:
Also check if you are making any changes to the bin folder when running your web app.
check:
http://stackoverflow.com/questions/542369/application-start-firing-unexpectedly-asp-net
0
salalimosAuthor Commented:
see the code below

its on windows 2003, asp.net 2.0, its in the plesk default 2.0 pool,
since its win2003 im thinking its IIS 6

I am not changing any code, i just click on one of the links and i receive an email.


WHERE CAN I FIND THE APPLICATION POOL LOG?

Thanks.
   void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        firstRequest = true;
        
        string strBody = "Application Started";
        string fromEmail = "cccc@ccccccccc.com";
        System.Net.Mail.MailMessage Mail_ErrorReport = new System.Net.Mail.MailMessage();
        System.Net.Mail.MailAddress fromAdd = new System.Net.Mail.MailAddress(fromEmail);
        Mail_ErrorReport.From = fromAdd;            
        Mail_ErrorReport.To.Add("sss@ssss.com");
        Mail_ErrorReport.Subject = "Application Starting";
        Mail_ErrorReport.Body = strBody;     
                   
        System.Net.Mail.SmtpClient smtpserver = new System.Net.Mail.SmtpClient();        
     
        smtpserver.Host = "127.0.0.1";			
        smtpserver.Send(Mail_ErrorReport);
    }
    

Open in new window

0
Miguel OzSoftware EngineerCommented:
YOur code looks OK.

I will start checking:
1) If your code in any way modify the bin folder. (e.g your program log may log to this folder, or one of your dll files is leaking resources or the antivirus program)
2) Any timeouts or other web configuration issues that affect the lifetime of your application.

Logs depending on configuration can go to a file or to the event log.
Let me know if there are any related message in your event log when you run your web app.

How to Configure IIS logs:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/b344f84e-bc77-4019-859c-9d483bc85c77.mspx?mfr=true

Note: Sending an email every time will fill up your inbox with unneeded information. If none of the above helps you to solve this issue just create a variable with initial value false as per attached code.
private static bool HasAppStarted = false;
private readonly static object _syncObject = new object();

public void Application_Start(object sender, EventArgs e)
{
    if (!HasAppStarted)
    {
        lock (_syncObject)
        {
            if (!HasAppStarted)
            {
                // Run application StartUp code here
                firstRequest = true; 
         
                string strBody = "Application Started"; 
                string fromEmail = "cccc@ccccccccc.com"; 
                System.Net.Mail.MailMessage Mail_ErrorReport = new System.Net.Mail.MailMessage(); 
                System.Net.Mail.MailAddress fromAdd = new System.Net.Mail.MailAddress(fromEmail); 
                Mail_ErrorReport.From = fromAdd;             
                Mail_ErrorReport.To.Add("sss@ssss.com"); 
                Mail_ErrorReport.Subject = "Application Starting"; 
                Mail_ErrorReport.Body = strBody;      
                    
                System.Net.Mail.SmtpClient smtpserver = new System.Net.Mail.SmtpClient();         
      
                smtpserver.Host = "127.0.0.1";                   
                smtpserver.Send(Mail_ErrorReport);

                HasAppStarted = true;
            }
        }
    }
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.