Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 VS2010 WebService
I have a Web Service and i used timers inside it. when i run the service application inside my vs2010 it work's like a charm. but when i deploy it to the remote server it doesn't fire the ElapsedEventHandler. I used a Logger to log the events and i set the permissions for write into the log file. Let me explain more. I'm trying to write a service which waits for a job, and the job is sending email to a big list. and emails should be send by 20 secs interval. here is some part of my code:
public class EmailSender : IEmailSender
{
 
    Queue<AdvEmail> ListOfEmails = new Queue<AdvEmail>();
    List<EmailJob> EmailJobs = new List<EmailJob>();
 
    Timer timerFetchJobs = null;
    Timer timerSendEmails = null;
 
    public EmailSender()
    {
        try
        {
            Logger.Log("init service");
            timerFetchJobs = new Timer();
            timerFetchJobs.Interval = 5 * 1000;
            timerFetchJobs.Elapsed += new ElapsedEventHandler(timerFetchJobs_Elapsed);
            timerFetchJobs.Enabled = true;
            timerSendEmails = new Timer();
            timerSendEmails.Interval = 20 * 1000;
            timerSendEmails.Elapsed += new ElapsedEventHandler(timerSendEmails_Elapsed);
            timerSendEmails.Enabled = true;
            timerFetchJobs.Start();
            timerSendEmails.Start();
            Logger.Log("init service Ended Succefully");
            Logger.Log("timerFetchJobs.Enabled=" + timerFetchJobs.Enabled.ToString());
            Logger.Log("timerSendEmails.Enabled=" + timerSendEmails.Enabled.ToString());
        }
        catch (Exception ex)
        {
            Logger.Log("Error in InitService. Message: " + ex.Message);
        }
    }
 
    void timerSendEmails_Elapsed(object sender, ElapsedEventArgs e)
    {
        timerSendEmails.Stop();
        Logger.Log("Checking for Emails in the Queue");
        // some statments here

        timerSendEmails.Start();
    }
 
    void timerFetchJobs_Elapsed(object sender, ElapsedEventArgs e)
    {
        timerFetchJobs.Stop();
        try
        {
            FetchJobsFromDB();
        }
        catch (Exception ex)
        {
            Logger.Log("Error in timerFetchJobs. Message: " + ex.Message);
        }
        timerFetchJobs.Start();
    }
when i go to the service addreess from browser, it initializes and logs the init level correctly, but no clue of firing timers!! which i dont know why?? what I'm doing wrong? thanks
 
Edit: here is the log file when i run it locally:
 
Log Entry : 1392/01/30 08:47:09 : :init service-------------------------------
 
Log Entry : 1392/01/30 08:47:09 : :init service Ended Succefully-------------------------------
 
Log Entry : 1392/01/30 08:47:09 : :timerFetchJobs.Enabled=True-------------------------------
 
Log Entry : 1392/01/30 08:47:09 : :timerSendEmails.Enabled=True-------------------------------
 
Log Entry : 1392/01/30 08:47:14 : :Checking for Jobs in the Database-------------------------------
 
Log Entry : 1392/01/30 08:47:16 : :There is a new job. job name is Pad 71-------------------------------
 
Log Entry : 1392/01/30 08:47:19 : :Adding Emails for th job Pad 71-------------------------------
 
And here is the log file when i deploy it to remote server
 
Log Entry : 1392/01/30 08:47:09 : :init service-------------------------------
 
Log Entry : 1392/01/30 08:47:09 : :init service Ended Succefully-------------------------------
 
Log Entry : 1392/01/30 08:47:09 : :timerFetchJobs.Enabled=True-------------------------------
 
Log Entry : 1392/01/30 08:47:09 : :timerSendEmails.Enabled=True-------------------------------
Posted 20-Apr-13 5:11am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

System.Timers.Timer has a AutoReset property which you should set to True.
  Permalink  
Comments
mahdi87_gh at 20-Apr-13 13:06pm
   
thanks buddy. i did that but no change. I'm wondering why it doesn't work. it works perfectly on my vs2010. and another point is, it must run at least for one time!!! and i get the "Checking for Jobs in the Database" log. am i right?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I changed logger class to log in a string variable and add a operation contract to get the log and the problem gone!!!
 
now i have another problem. after a while the timers stop!! seems when there is no new request, IIS shuts down the service! how can i have a infinitive time service?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100