This seemed to me a pretty good article on setting up a new Windows Service. I don't have the need to do so, but I've wondered how I might set one up anew from nothing. A class which subclasses ServiceBase is the cornerstone of such an application. One thing that messed with my mind was the fact that such a class had a different icon as if it were made in Visual Studio from a means other than just creating a new class. The artice I give a link too suggests that is not the case.
using System.ServiceProcess;
namespace AreYouBeingServed
{
class FooService : ServiceBase
{
private System.Diagnostics.EventLog eventLog;
public FooService()
{
this.eventLog = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog)).BeginInit();
this.eventLog.Log = "Application";
this.eventLog.Source = "FooService";
this.ServiceName = "FooService";
((System.ComponentModel.ISupportInitialize)(this.eventLog)).EndInit();
}
protected override void OnStart(string[] args)
{
eventLog.WriteEntry("service started");
}
protected override void OnStop()
{
eventLog.WriteEntry("service is stopping");
}
public void Debug()
{
//whatever
}
}
}
I made the class above in a console application. My Program.cs is below. I have no idea if this would run or not. It is just an example.
namespace AreYouBeingServed
{
class Program
{
static void Main(string[] args)
{
#if(!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new FooService() };
ServiceBase.Run(ServicesToRun);
#else
FooService myService = new FooService();
myService.Debug();
#endif
}
}
}
No comments:
Post a Comment