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.
Related
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.
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;
}
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.
How do I convert a command prompt app to a windows service? So far this is what I have, but when I try to install it with InstallUtil.exe I'm received an error:
No public installers with the RunInstallerAttribute.Yes attribute could be found in the ....
I didn't know I had to create an installer class and I'm not sure how to go about this. Can someone help me by telling me how to write an installer class so that I can install my app as a windows service?
class Program
{
public const string ServiceName = "ProcessingApp";
public class Service : ServiceBase
{
public Service()
{
ServiceName = Program.ServiceName;
}
protected override void OnStart(string[] args)
{
Program.Start(args);
}
protected override void OnStop()
{
Program.Stop();
}
}
private static void Start(string[] args)
{
// onstart code here
StartCode();
}
private static void Stop()
{
// onstop code here
ServiceController service = new ServiceController(ServiceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(100000);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
catch
{
}
}
static void Main(string[] args)
{
if (!Environment.UserInteractive)
// running as service
using (var service = new Service())
ServiceBase.Run(service);
else
{
// running as console app
Start(args);
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Stop();
}
For that to work, you need to have a class that inherits from System.Configuration.Install.Installer in System.Configuration.Install.dll. TThe constructor should configure a ServiceProcessInstaller and a ServiceInstaller, and add them both to the Installers collection - setting the Account, StartType, ServiceName, Description, etc.
MSDN has an example: https://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller(v=vs.110).aspx
Is it possible to have an application that runs as a service if it is registered as such but if it is double clicked simply starts a regular interactive application?
Yes. You can use the Environment.UserInteractive variable. You will need to create a small wrapper around your service to expose the OnStart() and OnStop() methods since they are protected.
var service = new MyService();
if (Environment.UserInteractive)
{
service.Start(args);
Console.WriteLine("Press any key to stop program");
Console.Read();
service.Stop();
}
else
{
ServiceBase.Run(service);
}
Wrapper Class (Make sure to extend ServiceBase)
public partial class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
//start code
}
protected override void OnStop()
{
//stopcode
}
public void Start(string[] args)
{
OnStart(args);
}
}