473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Windows Service Error

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
7 1527
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
Unfortunately still no difference :(

Oct 8 '07 #3

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

<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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MatthewRoberts | last post by:
Howdy All, I have a Windows Service that often stops in its tracks with no exception and no explanation on our QA system. During testing on the development machine, it can handle any workload,...
0
by: Martijn Remmen | last post by:
I have developed a service which exposes a COM object. This service is running perfect on Windows 2000 Server and Windows 2000 Professional under the SYSTEM account. When the service is...
9
by: Hardy Wang | last post by:
Hi all: I read an article from http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp about how to install a windows service programmatically. Based ont the code...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
5
by: Richard Steele | last post by:
I have created a WinForm application that needs to be run as a windows service (the PC is inaccesible by any user) i have successfully installed the application as a windows service. When i start...
5
by: R. Ian Lee | last post by:
I'm using the System.Web.Mail.SmtpMail object (.NET 1.1) to send email from an ASP.NET application. Everything works fine when I run it from Windows 2000 Server, but not from WinXP Pro. Is it...
6
by: Dean R. Henderson | last post by:
I have a DTS Package I am able to execute successfully from a Windows Form application, but I cannot get this to work from an ASP.NET Web Service, although the Web Service impersonates the same...
10
by: Ger | last post by:
I am having problems using VB.Net's Management base object on a machine hosting Windows Server 2003. I am trying to set file permissions from a Windows Service. These files may be loacted on a...
7
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch...
1
by: TJ | last post by:
I created a windows service using VS2005. I created/adjusted the install class using the designer controls. I set the user to be 'local system'. I installed the service using the VS2005 cmd...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.