none
Windows service going idle RRS feed

  • Question

  • I have a windows service that communicates with a DB residing on a different server. The service is installed and always running. The service will look out if there any new records in the DB Table and get 1000 records at a time via stored proc and process records (updates/creates in the CRM system). Logic is working perfectly alright but the problem is with service going to idle state (this service shows as running in windows services  but won't execute the method 'ProcessNewOtranRecords' that calls stored proc) after few hours. When the service is restarted it again works as expected for few more hours.

    Please suggest me if there is any good approach to keep service active all the time.

    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.Security.Permissions; 
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
    using CRM.Objects.BusinessLogic; 
    
    namespace CRM
    {
        partial class CrmProcessOtran : ServiceBase
        {
            OtranBL _otranBL = new OtranBL();
            private System.Timers.Timer mainTimer;
            int eventID = 0;
    
            public CrmProcessOtran()
            {
                InitializeComponent();
    
            }
    
            protected override void OnStart(string[] args)
            {
                eventLog1.WriteEntry("Service Start");
                
                mainTimer = new System.Timers.Timer(30000); // 30 seconds
                mainTimer.Elapsed += PerformOtranOperations;
                mainTimer.AutoReset = false;
                mainTimer.Start(); 
            }
    
    
            protected override void OnStop()
            {
                eventLog1.WriteEntry("Service Stopped");
                
                mainTimer.Stop();
                mainTimer.Dispose();
                mainTimer = null; 
            }
    
            public void PerformOtranOperations(object sender, EventArgs e)
            {
                eventID++; 
                eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + " - checking for new otran records", EventLogEntryType.Information, eventID);
                // Check for new otran records 
                int otranRecords = _otranBL.GetOtranRecordCount(eventLog1);
                if (otranRecords == 0)
                {
                    eventLog1.WriteEntry("0 new otran records", EventLogEntryType.Information, eventID);
                    return;
                }
    
                eventLog1.WriteEntry(otranRecords.ToString("N0") + " new otran records found with proc_status = 0", EventLogEntryType.Information, eventID);
                // Process new records 
                eventLog1.WriteEntry(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss") + " - Begin processing new otran records", EventLogEntryType.Information, eventID);
                int processedrecords = _otranBL.ProcessNewOtranRecords(eventLog1);
                eventLog1.WriteEntry(processedrecords.ToString() + " processed records", EventLogEntryType.Information, eventID);
    
                mainTimer.Start();
            }
        }
    }

    Thanks..

    • Edited by Ram.Crm Wednesday, May 23, 2018 2:11 PM
    Wednesday, May 23, 2018 1:52 PM

Answers

  • Hi Ram,

    In you app. config file please add the line. Also look into the below thread answer for more information. Hope this will help you.

    <reliableSession ordered="true" inactivityTimeout="20:10:00"
                            enabled="false" />

    KeepAlive with WCF and TCP?


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, May 23, 2018 8:20 PM
    Moderator

All replies

  • Hi Ram,

    In you app. config file please add the line. Also look into the below thread answer for more information. Hope this will help you.

    <reliableSession ordered="true" inactivityTimeout="20:10:00"
                            enabled="false" />

    KeepAlive with WCF and TCP?


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, May 23, 2018 8:20 PM
    Moderator
  • Humm... nothing in the question suggested it's a WCF thing.

    Btw, I've written a lot of Windows service with thread timer before, and I can't remember any time I need that to work.

    I suggest you wrap the content of PerformOtranOperations() with try...finally... block, and put mainTimer.Start() inside finally. There could be exception thrown inside the method so it'll never run to mainTimer.Start() again.

    Thursday, May 24, 2018 1:31 AM
    Answerer

  • Yes, the problem is not starting the timer. The code doesn't start the timer when the number of records is 0. It returns without executing mainTimer.Start();
    Thursday, May 24, 2018 6:52 AM