Windows Service Timeout then gives error 1053 - c#

I have developed a C# Windows service to get some datas from DB and process them in an infinite loop. The service was working fine with nothing but loop in it yesterday but today I have finished the development and tried to test it as a Windows service but it keep says Starting and when the green bar is complete it gives me "1053" error. I have checked if there are any logs and my service is inserting logs and even processing Datas but somehow I still get this error.
I have installed the service from my release folder. There is no error on Event Viewer regarding the service. And my service looks like below.
*UPDATE: When I check event viewer I see below messages in a sequence; "Session 1 started", "Ending Session 1" "Machine restart required". I have tried restarting but it didn't make any difference
Program.cs
static class Program
{
static void Main()
{
try
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new spService()
};
ServiceBase.Run(ServicesToRun);
}
catch (Exception ex)
{
EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
}
}
}
Service1.cs
public partial class spService: ServiceBase
{
public spService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
SpPushOperation spo = new SpPushOperation();
spo.StartSpPushOperation();
}
protected override void OnStop()
{
SpPushOperation spo = new SpPushOperation();
spo.StopSpPushOperation();
}
}
SpPushOperation.cs
class SpPushOperation
{
public readonly NLog.ILogger Logger = NLog.LogManager.GetCurrentClassLogger();
public void StartSpPushOperation()
{
try
{
Logger.Info("-------------");
Logger.Info("SpPushOperation Started..");
Logger.Info("-------------");
while(true)
{
//some process in here
}
}
catch(Exception e)
{
Logger.Info("!!!!!!!!!!!!!!");
Logger.Info("Error on getting StartSpPushOperation Error: " + e);
Logger.Info("!!!!!!!!!!!!!!");
}
}
}
Any help would be appreciated. Thanks.

Issue was caused due to using infinite loop in service. To fix this issue instead of using infinite loop start the service with a thread and run for every 60 seconds.

Related

Windows service onStart() isn't executing

I have a windows service that is having trouble executing the Onstart() method after installing it and running it from the service menu.I'm logging everything that is happening at every step of the execution to see where the problem is. But there is no error, it logs and runs fine in the main method , up until the service is actually called to run and then it just does nothing.
It's interesting to note that it doesn't have ANY problem running in debug.
My program class(starting point) from which the service is called :
public class Program
{
private static Container _container;
private static ILogger<Program> _logger;
/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main()
{
_container = SimpleInjectorContainer.Build(registerConfig: true, useThreadScopedLifestyle: true);
SimpleInjectorContainer.LoggAndVerify(_container);
using (ThreadScopedLifestyle.BeginScope(_container))
{
try
{
_logger = _container.GetInstance<ILogger<Program>>();
_logger.LogInformation("Test - Works");
VerifyConfiguration();
}
catch (Exception ex)
{
var logger = _container.GetInstance<ILogger<Program>>();
logger.LogError(ex, "Configuration is not valid");
throw;
}
if (Environment.UserInteractive)
{
RunDebug();
}
else
{
System.Diagnostics.Debugger.Launch();
_logger.LogInformation("It's Here 49");
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
_container.GetInstance<SenderService>()
};
_logger.LogInformation("It's Here 56");
//up until here its fine, but it doesn't run the next line
ServiceBase.Run(ServicesToRun);
_logger.LogInformation("It's Here 58");
}
}
}
private static void RunDebug()
{
var senderService = _container.GetInstance<SenderService>();
senderService.TestStart();
Console.WriteLine("Sender Started Debug");
Console.ReadLine();
senderService.TestStop();
}
private static void VerifyConfiguration()
{
var configValidator = _container.GetInstance<IConfigurationValidator>();
configValidator.VerifyOperatorPrefixNumbers();
configValidator.VerifyConfiguration();
configValidator.VerifyOperators();
}
}
My actual service :
public partial class SenderService : ServiceBase
{
private readonly Container container;
private readonly ILogger<SenderService> logger;
private readonly ISmsHandlerConfig config;
private readonly IConfigurationValidator configValidator;
public SenderService(
Container container,
ILogger<SenderService> logger,
ISmsHandlerConfig config,
IConfigurationValidator configValidator)
{
this.InitializeComponent();
this.container = container;
this.logger = logger;
this.config = config;
this.configValidator = configValidator;
}
public void TestStart()
{
Console.WriteLine($"Starting {ServiceName} service");
this.OnStart();
}
public void TestStop()
{
Console.WriteLine($"Stopping {ServiceName} service");
this.OnStop();
}
protected void OnStart()
{
try
{
this.logger.LogInformation($"{this.ServiceName} starting");
SmsHandlerAction();
}
catch (Exception ex)
{
this.logger.LogError(ex, $"Error starting service {this.ServiceName}");
throw;
}
}
protected override void OnStop()
{
try
{
this.Dispose();
this.logger.LogInformation($"{this.ServiceName} stopped");
}
catch (Exception ex)
{
this.logger.LogError(ex, $"Error stopping service {this.ServiceName}");
}
}
private void SmsHandlerAction()
{
while (true)
{
this.logger.LogInformation($"{this.ServiceName} started");
using (ThreadScopedLifestyle.BeginScope(this.container))
{
var smsSenderService = this.container.GetInstance<ISmsSenderService>();
var sendResult = smsSenderService.SendSms(this.container);
// Wait if there are not messages for sending
if (!sendResult && this.config.IdleTimeMiliseconds != 0)
{
Thread.Sleep(this.config.IdleTimeMiliseconds);
}
}
}
}
}
this is what is logged:
2019-02-12 18:02:18.7972 INFO Test - Works
2019-02-12 18:02:20.6370 INFO It's Here 49
2019-02-12 18:02:20.6410 INFO It's Here 56
and after I stop the service :
2019-02-12 18:02:35.7375 INFO SenderService stopped
2019-02-12 18:02:35.7375 INFO It's Here 58
It missing the this.logger.LogInformation($"{this.ServiceName} starting"); part.
It doesn't log the line in the onstart method , because it never actually executes, I checked if the service was running, but just failed to log and that is not the case.
My IDE is VS 2017, OS is Win 7, DI library is SimpleInjector 4.0.12.
I know about a similar question asked on stackoverfllow(this) but I don't see how it solves my problem.
Also my event viewer doesn't log any problems also, only information about it starting successfully.
I'm pretty lost so any guidance will be of help.
I found the problem, it was because I removed the override on the OnStart().

How to write events from multiple applications(sources) to the same event log?

I have created a custom event log and would like all my applications to write to the same event log. As you can see below in the image attached, DistributedCOM and Svc Ctrl Mgr are 2 sources writing to the same event log System.
Similarly, I have 2 services that I want to write to the same eventLog.
I tried doing that by creating one event log and passing different source names from the 2 Windows Services that I have created. But I find only one Service writing to the log while the other doesn't.
Below is the class library that I created for Event Log.
public class EventLogger
{
private EventLog eventLog1 = new System.Diagnostics.EventLog();
public EventLogger(string logSource)
{
if (!System.Diagnostics.EventLog.SourceExists(logSource))
{
System.Diagnostics.EventLog.CreateEventSource(logSource, "SampleLog");
}
eventLog1.Source = logSource;
eventLog1.Log = "SampleLog";
}
public void WriteLog(string message)
{
eventLog1.WriteEntry(message);
}
Created 1st Windows Service
public partial class Service1 : ServiceBase
{
private EventLogger.EventLogger eventLog;
public Service1()
{
InitializeComponent();
eventLog = new EventLogger.EventLogger("WinService1");
}
protected override void OnStart(string[] args)
{
try
{
eventLog.WriteLog("Service started in 1st");
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.ToString());
}
}
protected override void OnStop()
{
eventLog.WriteLog("Service stopped");
}
}
Created the 2nd windows Service as well, as above.
public partial class Service2 : ServiceBase
{
private EventLogger.EventLogger eventLog;
public Service2()
{
InitializeComponent();
eventLog = new EventLogger.EventLogger("WinService2");
}
protected override void OnStart(string[] args)
{
try
{
eventLog.WriteLog("Service started in 2nd");
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.ToString());
}
}
protected override void OnStop()
{
eventLog.WriteLog("Service stopped");
}
}
Service1 doesn't seem to log anything, whereas, I can see the logs for Service2. I might be doing a lot of things incorrectly here. Please help me in finding a solution to this. Also, if this can be achieved by using log4Net then solutions with respect to that are welcome as well. thanks in advance.
EDIT: Also, when I try to stop the services, Service 1 fails to stop and throws an error. Image given below.
EDIT 2: Just changed the constructor of the EventLogger class as below and then it worked!! I am not entirely sure if this was the actual cause for the improper functioning. And I'm not quite sure if it had anything to do with the setting of the Log property either. Any light thrown on this by any one of you would be appreciated. I would like to understand better as to what exactly happened here. Thanks.
string logName = "NewLog";
public EventLogger(string logSource)
{
if (!System.Diagnostics.EventLog.SourceExists(logSource))
{
System.Diagnostics.EventLog.CreateEventSource(logSource, logName);
}
eventLog1.Source = logSource;
}

WIndow Service onStart() can't invoke another meathod in which mongoDB distinct query result expected

I'm working on a function which need to run every hour and i adopted service way to write. but now i'm facing issues in running service while in debug mode help.
every thing looks fine to me, code start from debug section and calculation started but here is the problem when i try to get the distinct elements from db the service code stops.
//main
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Service1 myService = new Service1();
myService.onDebug();
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
}
//*********************************************************
//Service.cs code is here
public Service1()
{
InitializeComponent();
}
public void onDebug()
{
OnStart(null); //untill now working fine
}
protected override void OnStart(string[] args)
{
try
{
//this is the function i want to run every hour, i have made successfully perfect in a console application but in window service its not working
var hourlyTimer = new System.Threading.Timer(e => hourlyCalculateSummaryForAllDevices(), null, TimeSpan.FromMinutes(0), TimeSpan.FromMinutes(0));
}
catch (Exception ex)
{
string msg = ex.Message;
}
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "ServiceStarted.txt");
}
protected override void OnStop()
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory + "HourlySummaryServiceStopped.txt");
}
//*********************************************************
//hourly Summary calculation functions
public static void hourlyCalculateSummaryForAllDevices()
{
findDistinctAssets(); //ok f11 pressed when cursor come here
}
static async void findDistinctAssets()
{
var collection = _database.GetCollection<BsonDocument>(telemetryCollectionName);
var filter = new BsonDocument();
var cursor = await collection.DistinctAsync<Int64>("AssetName", filter);
//while (await cursor.MoveNextAsync()) //here the service code stopped automatically, im stuck why?
//{
var assets = cursor.Current;
foreach (var asset in assets)
{
CalculateSummaryOfAssets(Convert.ToInt64(asset));
}
Console.Read();
//}
}

Can't start service, error 1053, made in Visual Studio, c#

So i've made a small service program, but it won't start.
It installs by itself, but I don't have it in auto-start.
It's on a windows 7, 64 bit system.
When I find it in Services, right click the service and have it start, it times out with the error 1053 after about 30 seconds.
I am running the program as release, and not debug.
Tried to install as local admin and do everything as local admin.
The OnStart() and OnStop() methods are empty with no code, I removed it all to eliminate what it could be.
Tried putting a small logging action that I know works(I use it to create a log file when the install is successful) at the start of OnStart() but it never reaches it.
Help?
Edit:
Here is my Program.cs code:
namespace TestService
{
static class Program
{
// The main entry point for the application.
static void Main()
{
//Install self
SelfInstaller.InstallMe();
}
}
}
Here is my Library.cs:
namespace TestService
{
//Library to store public methods
public static class Library
{
//Method to write to a logfile
public static void WriteLogFile(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ": " + Message.ToString());
sw.Flush();
sw.Close();
}
catch
{
//empty
}
}
}
}
Here is my Service1.cs:
namespace TestService
{
public partial class Service1 : ServiceBase
{
//Initialize
public Service1()
{
InitializeComponent();
}
//On service start
protected override void OnStart(string[] args)
{
}
//On service stop
protected override void OnStop()
{
}
}
}
It seems like you found a tutorial but followed only half of it.
Your current main() code will install the service every time you try to start it:
static void Main()
{
//Install self
SelfInstaller.InstallMe();
}
So that won't let the ServiceManager know the service has been started - as it isn't.
You need to decide, in main(), whether you want to start, install, uninstall or debug the service. It's common to do so using command-line arguments, where no arguments supplied means "start the service".
How to do this is also shown in that very tutorial.

ServiceBase OnStart Not receiving args from serviceController.Start

As I understand it, the serviceBase.Onstart(args) should recieve arguments that are present in serviceController.start(args).
Here is my service controller implementation
if (serviceController.Status == ServiceControllerStatus.Stopped)
{
try
{
string[] args = {"execute-service"};
serviceController.Start(args);
ServiceManager.WaitForStatusChange(......);
}
catch (InvalidOperationException ex)
{
EventLog.WriteEntry(.....);
return 1;
}
}
Here is my service.cs
protected override void OnStart(string[] args)
{
this.OnStart(args);
this.scheduler.StartScheduler();
}
When I attempt to start my service, the argument "execute-service" is not passed to the main program.cs. I have a logFile that is being created and can see the args are not there.
Looking for some ideas on how to pass the args, as I read online, I am doing it correctly using the servicebase.onstart().
Thoughts on how to debug or fix?
I don't think there is enough information shown here to debug this issue. For comparison, this is what I do in a console application designed to run as a Windows service:
public sealed partial class ServiceMain : ServiceBase
{
// Service startup modes.
private const string DEBUG = #"/d";
private const string INSTALL = #"/i";
private const string UNINSTALL = #"/u";
Then ServiceMain.Main() is set is set as the startup method:
// This is the entry point for this service.
// This method runs on an SCM thread.
public static void Main(string[] args)
{
if (Environment.UserInteractive && args.Length > 0)
{
switch (args[0])
{
// Debug the service as a normal app, presumably within Visual Studio.
case DEBUG:
ServiceMain DebugService = new ServiceMain();
DebugService.OnStart(null);
break;
Which calls the ServiceMain.OnStart() method:
// SCM requests service start using its own thread.
// This method must complete within 10 seconds of it
// starting. Otherwise the SCM diagnoses a hang.
protected override void OnStart(string[] args)
{
this.AppLog = new Log();
AppLog.Information("SCM requested service start.", "ServiceMain.OnStart");
You can see the whole service in context here.

Categories