D
dm3281
I'm really starting to hate writing services -- or trying to, anyway.
Why do I need to rename my project to the service name?
Why do I need to set the "ServiceName" property to my service name?
Why do I need to set a property within my code to the service name?
Are all these required or am I just doing this for consistency purposes?
Now for my real question/problem:
I have written this service and have this in my "SimpeService.cs":
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
namespace WindowsService2
{
public partial class SimpleService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer;
/// <summary>
/// Required deisgner variable
/// </summary>
public SimpleService()
{
// This call is required by the Windows.Forms Component Designer
InitializeComponent();
this.timer = new System.Timers.Timer();
this.timer.Enabled = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed);
CanPauseAndContinue = true;
this.ServiceName = "Hello-World Service";
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("Hello World!");
//throw new Exception("The method or operation is not
implemented.");
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteEntry("Hello-World Service Started");
timer.Enabled = true;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
EventLog.WriteEntry("Hello-World Service Paused");
timer.Enabled = false;
}
protected override void OnPause()
{
EventLog.WriteEntry("Hello-World Service paused");
timer.Enabled = false;
}
protected override void OnContinue()
{
EventLog.WriteEntry("Hello-World Service continued");
timer.Enabled = true;
}
}
}
This is in my "ProjectInstaller.cs" and I have set the service properties,
etc:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace WindowsService2
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public ProjectInstaller()
{
InitializeComponent();
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
// Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Hello-World Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
My "Program.cs" has no changes:
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
namespace WindowsService2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process.
To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new
MySecondUserService()};
//
ServicesToRun = new ServiceBase[] { new SimpleService() };
ServiceBase.Run(ServicesToRun);
}
}
}
Now, when I compiel the above, it all compiles fine.
I then jump to Admin Command Prompt and do a "installutil
WindowsService2.exe" and it does the following:
It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the "ProjectInstaller.cs"
file. Why do I get this?
The format of the credentials appears to only accept "machinename\username"
and nothing else. It took me a while to figure this out! Arrghhh
After the service is installed, I appear to have two services:
SimpleService2
Hello-World
SimpleService2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.
Executing "InstallUtil -u WindowsService2.exe" appears to get rid of both
services.
I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService.cs" file??
I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.
How do I get rid of the dialoge all together?
How do I get rid of "SimpleService" installation, since it appears to not
work anyway.
Any help would be appreciated.
Why do I need to rename my project to the service name?
Why do I need to set the "ServiceName" property to my service name?
Why do I need to set a property within my code to the service name?
Are all these required or am I just doing this for consistency purposes?
Now for my real question/problem:
I have written this service and have this in my "SimpeService.cs":
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
namespace WindowsService2
{
public partial class SimpleService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer;
/// <summary>
/// Required deisgner variable
/// </summary>
public SimpleService()
{
// This call is required by the Windows.Forms Component Designer
InitializeComponent();
this.timer = new System.Timers.Timer();
this.timer.Enabled = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed);
CanPauseAndContinue = true;
this.ServiceName = "Hello-World Service";
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("Hello World!");
//throw new Exception("The method or operation is not
implemented.");
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteEntry("Hello-World Service Started");
timer.Enabled = true;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
EventLog.WriteEntry("Hello-World Service Paused");
timer.Enabled = false;
}
protected override void OnPause()
{
EventLog.WriteEntry("Hello-World Service paused");
timer.Enabled = false;
}
protected override void OnContinue()
{
EventLog.WriteEntry("Hello-World Service continued");
timer.Enabled = true;
}
}
}
This is in my "ProjectInstaller.cs" and I have set the service properties,
etc:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace WindowsService2
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public ProjectInstaller()
{
InitializeComponent();
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
// Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Hello-World Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
My "Program.cs" has no changes:
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
namespace WindowsService2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process.
To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new
MySecondUserService()};
//
ServicesToRun = new ServiceBase[] { new SimpleService() };
ServiceBase.Run(ServicesToRun);
}
}
}
Now, when I compiel the above, it all compiles fine.
I then jump to Admin Command Prompt and do a "installutil
WindowsService2.exe" and it does the following:
It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the "ProjectInstaller.cs"
file. Why do I get this?
The format of the credentials appears to only accept "machinename\username"
and nothing else. It took me a while to figure this out! Arrghhh
After the service is installed, I appear to have two services:
SimpleService2
Hello-World
SimpleService2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.
Executing "InstallUtil -u WindowsService2.exe" appears to get rid of both
services.
I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService.cs" file??
I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.
How do I get rid of the dialoge all together?
How do I get rid of "SimpleService" installation, since it appears to not
work anyway.
Any help would be appreciated.