|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAt the beginning, there was no easy way to build a Windows service using Visual Studio and .NET Framework. Now that Microsoft has integrated this functionality as a template and a class library, it's much more easy to do that. This is good, but to test your newly created service you have to deploy it. That means you must add an installer and build a whole MSI file and run it in your environment. Once deployed, you can start it with Windows Service controller and attach a debugger to it. I don't know how this sounds to you, but to me it is too much unneeded work to do a quick test. This article describes a much more easy way to do that. Using the CodeYou will find a sample application using this concept. Here is how it works. First you need a working Windows service. (For more information, refer to this.) In Visual Studio, building a Web service is easy. Create a new project and select Windows Service template. Then add By default, when you create a Windows service project, Visual Studio will create two classes: a static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun);
}
}
The first thing we have to do is to surround that code with a conditional statement to let us control if we want to run as a real service or just in debugging mode. static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
if (args.Length > 0 && args[0].ToLower().Equals("/debug"))
{
Application.Run(new ServiceRunner(new Service1()));
}
else
{
ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun);
}
}
}
In this new Go to Project -> ... Properties -> Debug and type /debug in the command line arguments field. From there, you have two options to make your service debuggable. Either you implement Implementing IDebuggableService InterfaceIf you already subclass public interface IDebuggableService
{
void Start(string[] args);
void StopService();
void Pause();
void Continue();
}
Notice that the Here is a sample implementation of this interface: public partial class Service1 : ServiceBase, IDebuggableService
{
//
// Your existing service code
//
// ...
//
#region IDebuggableService Members
public void Start(string[] args)
{
OnStart(args);
}
public void StopService()
{
OnStop();
}
public void Pause()
{
OnPause();
}
public void Continue()
{
OnContinue();
}
#endregion
}
Subclassing DebuggableServiceSubclassing your service from public partial class Service1 : DebuggableService
{
//
// Your existing service code
//
// ...
//
}
All the control methods are already implemented by Points of InterestIn my sample code, I added a Windows Form class to let you start, stop and pause your service. You can use it as is, build your own controller interface or just call the methods in your program class. It is up to you. Of course you should still deploy your service to give it the final test run. One good reason to do this is to test the actual security context in which your service will run. It will not have the same permission as when you run it in Visual Studio, but this technique can speed up the development process.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||