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 2016). 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(); } } }
*This post is locked for comments