By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,542 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,542 IT Pros & Developers. It's quick & easy.

Windows Service Error

P: n/a
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(ServicesToRu n);
}

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;
}
}

Oct 8 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

<d.***********@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
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(ServicesToRu n);
}

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;
}
}
Oct 8 '07 #2

P: n/a
Unfortunately still no difference :(

Oct 8 '07 #3

P: n/a

<d.***********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
Unfortunately still no difference :(
What O/S are you using?

Oct 9 '07 #4

P: n/a
Can you show the revised code that has the logging in place?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<d.***********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
Unfortunately still no difference :(

Oct 9 '07 #5

P: n/a
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("MySour ce"))
{
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();
}
}
}

Oct 9 '07 #6

P: n/a

<d.***********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
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.

Oct 10 '07 #7

P: n/a
<d.***********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
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("MySour ce"))
{
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.
Oct 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.