Windows Service Error

D

d.s.stevenson

Hi,
I have the following code running as a windows service, but it just
won't work... trying to run the service as 'adminuser', and i have
made sure it logs on as 'adminuser' in the Services console of
Computer Management. There are no errors coming up in the event viewer
either. Any ideas?

################
## Service1.cs
################
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Xml;
using System.Security;
using System.Timers;

namespace testservice
{
public partial class Service1 : ServiceBase
{
Timer timer = new Timer();

public Service1()
{
this.ServiceName = "CCIPVulnerabilityService";
this.EventLog.Log = "Application";

this.CanHandlePowerEvent = false;
this.CanHandleSessionChangeEvent = false;
this.CanPauseAndContinue = false;
this.CanShutdown = false;
this.CanStop = true;
}

public void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
timer.AutoReset = true;
timer.Interval = 120000;
timer.Elapsed += new ElapsedEventHandler(DoWork);
timer.Enabled = true;
timer.Start();
}

public void DoWork(object source, ElapsedEventArgs e)
{
eventLog1.WriteEntry("doing work");
}

protected override void OnStop()
{
eventLog1.WriteEntry("my service stopped");
}
}
}

####################
## testserviceinstaller.cs
####################
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace testservice
{
[RunInstaller(true)]
class testserviceinstaller : Installer
{
public testserviceinstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new
ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new
ServiceInstaller();

serviceProcessInstaller.Account = ServiceAccount.User;
serviceProcessInstaller.Username = "DOMAIN\\adminuser";
serviceProcessInstaller.Password = "ExampleP@55word";

serviceInstaller.DisplayName = "CCIPVulnerabilityService";
serviceInstaller.StartType = ServiceStartMode.Automatic;

serviceInstaller.ServiceName = "CCIPVulnerabilityService";

this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}

################
## Service1.Designer.cs
################
namespace testservice
{
partial class Service1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
this.testserviceinstaller1 = new
testservice.testserviceinstaller();
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).BeginInit();
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).EndInit();

}

#endregion

private System.Diagnostics.EventLog eventLog1;
private testserviceinstaller testserviceinstaller1;
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

I would try and surround the code in the DoWork and in the OnStart
methods in an exception handler (try/catch) which will log any exceptions,
and see what comes up.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I have the following code running as a windows service, but it just
won't work... trying to run the service as 'adminuser', and i have
made sure it logs on as 'adminuser' in the Services console of
Computer Management. There are no errors coming up in the event viewer
either. Any ideas?

################
## Service1.cs
################
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Xml;
using System.Security;
using System.Timers;

namespace testservice
{
public partial class Service1 : ServiceBase
{
Timer timer = new Timer();

public Service1()
{
this.ServiceName = "CCIPVulnerabilityService";
this.EventLog.Log = "Application";

this.CanHandlePowerEvent = false;
this.CanHandleSessionChangeEvent = false;
this.CanPauseAndContinue = false;
this.CanShutdown = false;
this.CanStop = true;
}

public void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
timer.AutoReset = true;
timer.Interval = 120000;
timer.Elapsed += new ElapsedEventHandler(DoWork);
timer.Enabled = true;
timer.Start();
}

public void DoWork(object source, ElapsedEventArgs e)
{
eventLog1.WriteEntry("doing work");
}

protected override void OnStop()
{
eventLog1.WriteEntry("my service stopped");
}
}
}

####################
## testserviceinstaller.cs
####################
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace testservice
{
[RunInstaller(true)]
class testserviceinstaller : Installer
{
public testserviceinstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new
ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new
ServiceInstaller();

serviceProcessInstaller.Account = ServiceAccount.User;
serviceProcessInstaller.Username = "DOMAIN\\adminuser";
serviceProcessInstaller.Password = "ExampleP@55word";

serviceInstaller.DisplayName = "CCIPVulnerabilityService";
serviceInstaller.StartType = ServiceStartMode.Automatic;

serviceInstaller.ServiceName = "CCIPVulnerabilityService";

this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}

################
## Service1.Designer.cs
################
namespace testservice
{
partial class Service1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
this.testserviceinstaller1 = new
testservice.testserviceinstaller();
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).BeginInit();
//
// Service1
//
this.ServiceName = "Service1";
((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).EndInit();

}

#endregion

private System.Diagnostics.EventLog eventLog1;
private testserviceinstaller testserviceinstaller1;
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Can you show the revised code that has the logging in place?
 
D

d.s.stevenson

Using XP Pro SP2. I have made another service that effectively does
nothing except write to the event log. Code as follows. But it just
doesn't write anything to the event log... And no errors being output.

###############
## Service1.cs
###############

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace MyNewService
{
public partial class MyNewService : ServiceBase
{
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}

protected override void OnStop()
{
eventLog1.WriteEntry("In onStop");
}
}
}

#############
## Program.cs
#############

using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;

namespace MyNewService
{
static class Program
{

static void Main()
{
ServiceBase[] ServicesToRun;

ServicesToRun = new ServiceBase[] { new MyNewService() };

ServiceBase.Run(ServicesToRun);
}
}
}

############
## ProjectInstaller.cs
############

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;

namespace MyNewService
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
}
 
M

Mr. Arnold

Using XP Pro SP2. I have made another service that effectively does
nothing except write to the event log. Code as follows. But it just
doesn't write anything to the event log... And no errors being output.


Another approach you can take to debug the Service is enable XP's Messenger
Service and send messages to yourself as to where you're at in the Service
code by using the Netsend command with the machine's name. It will pop-up a
message box with message and OK button so you can see where you are at in
the code when it stops working.
 
W

Willy Denoyette [MVP]

Using XP Pro SP2. I have made another service that effectively does
nothing except write to the event log. Code as follows. But it just
doesn't write anything to the event log... And no errors being output.

###############
## Service1.cs
###############

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace MyNewService
{
public partial class MyNewService : ServiceBase
{
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}

protected override void OnStop()
{
eventLog1.WriteEntry("In onStop");
}
}
}

#############
## Program.cs
#############

using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;

namespace MyNewService
{
static class Program
{

static void Main()
{
ServiceBase[] ServicesToRun;

ServicesToRun = new ServiceBase[] { new MyNewService() };

ServiceBase.Run(ServicesToRun);
}
}
}

############
## ProjectInstaller.cs
############

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;

namespace MyNewService
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
}


This:

System.Diagnostics.EventLog.CreateEventSource(...

will fail unless the service runs in a local administrators account, are
your sure your service account is a member of administrators?
Note however that services should NOT create eventlog sources, this is
something that should be done by the service installer.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top