The easiest language to create a Windows service

What is the easiest language to create windows services?

The simplest in this case will be defined as the smallest amount of code and the lowest entry point into the language.

+49
windows-services
Feb 27 '09 at 3:56
source share
6 answers

If you pointed out the obvious, if you have C / C ++ / Java background, I think C # offers you the lowest entry point.

Assuming you are using Visual Studio 2008, you can do the following:

  • Open Visual Studio 2008 and select the menu item "File | New | Project".
  • In the New Project dialog box ...
    • Select Visual C # Types | Windows node in project types
    • Select a Windows Service Template
    • Enter a name and location for your project.
    • Click OK
  • At this point, you have all the basics for a Windows service. The Program.cs file contains the Main () method for your service, and Service1.cs defines the System.ServiceProcess.ServiceBase component, which is your new Windows service.
  • In the Property Grid component for your Service1 component, consider setting the following properties at a minimum:
    • (Name) - give an object an intuitive name, for example, ServiceExample
    • AutoLog - set to false to prevent the default events from being written to the application event log (Note: I am not saying that you should not log service events, I just prefer to write to my own event log instead of the application log - see below)
    • CanShutdown - set true if you want to handle system stops
    • ServiceName - Defines the name by which your service will be known to the Service Control Manager (SCM)
  • In the code for ServiceExample, the virtual functions OnStart () and OnStop () are crossed out. You will need to fill them with what is needed for your service. If you changed the CanShutdown property to true , you will also want to override the OnShutdown method. I gave an example illustrating the use of these functions.
  • At this point, the ServiceExample service is essentially complete, but you still need a way to install it. To do this, open the DesignExample component in the designer. Right-click anywhere in the designer panel and select the Add Installer menu item. This adds the ProjectInstaller component to your project, which contains two additional components: serviceProcessInstaller1 and serviceInstaller1.
  • Select the serviceProcessInstaller1 component in the designer. In the Property Grid, consider setting the following properties:
    • (Name) - give your object an intuitive name, for example serviceProcessInstaller
    • Account - Select a LocalService account at a minimum, but you may need to use a NetworkService or LocalSystem account if your service requires more privileges.
  • Select the serviceInstaller1 component in the designer. In the Property Grid, consider setting the following properties:
    • (Name) - give an object an intuitive name, for example, serviceInstaller
    • Description - a description of the service that will be displayed in SCM for your service.
    • DisplayName is a friendly name for your service that will be displayed in SCM for your service.
    • ServiceName - make sure that this is the same name that you selected for the ServiceName property of your ServiceExample component (see step 4).
    • StartType - Indicates whether you want the service to start automatically or manually.
  • Remember that I said that I prefer to write events to my own event log instead of the application event log. To do this, you will need to replace the default default EventLogInstaller in ProjectInstaller with a custom one. Make your code for ProjectInstaller as follows:



 using System.Diagnostics; [RunInstaller(true)] public partial class ProjectInstaller : Installer { public ProjectInstaller() { InitializeComponent(); EventLogInstaller installer = FindInstaller(this.Installers); if (installer != null) { installer.Log = "ServiceExample"; // enter your event log name here } } private EventLogInstaller FindInstaller(InstallerCollection installers) { foreach (Installer installer in installers) { if (installer is EventLogInstaller) { return (EventLogInstaller)installer; } EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers); if (eventLogInstaller != null) { return eventLogInstaller; } } return null; } } 

At this point, you can create your project to get the Windows service executable. To install the service, open a Visual Studio 2008 command prompt and change to the Debug or Release directory where your executable is located. At the command prompt, type the following: InstallUtil ServiceExample.exe . This will install your service on the local computer. To remove it, at a command prompt, type the following: InstallUtil / u ServiceExample.exe

While your service is not running, you can make changes to your service and rebuild, i.e. You do not need to delete your service to make changes to it. However, you will not be able to overwrite the executable with your patches and improvements while it is running.

To see your service in action, open the ServiceExample.cs file and make the following changes:

 using System.Diagnostics; public partial class ServiceExample : ServiceBase { public ServiceExample() { // Uncomment this line to debug the service. //Debugger.Break(); InitializeComponent(); // Ties the EventLog member of the ServiceBase base class to the // ServiceExample event log created when the service was installed. EventLog.Log = "ServiceExample"; } protected override void OnStart(string[] args) { EventLog.WriteEntry("The service was started successfully.", EventLogEntryType.Information); } protected override void OnStop() { EventLog.WriteEntry("The service was stopped successfully.", EventLogEntryType.Information); } protected override void OnShutdown() { EventLog.WriteEntry("The service was shutdown successfully", EventLogEntryType.Information); } } 

After starting the service with these changes, you can look at the ServiceExample event log in the event viewer and see the logged messages there.

Hope this helps.

EDIT: If you prefer to use the application event log for event logging instead of custom, just do not make any changes to the ProjectInstaller.cs file. Also, leave a line that sets the EventLog log property in the ServiceExample constructor. When the service starts, log messages will appear in the application event log.

+163
Feb 27 '09 at 7:08
source share

I agree with everyone who answered elsewhere, but I would say don’t focus too much on the actual language if you are working in the .NET framework and you have a starter project, it’s good to go. In the past, I made several “Windows services” and developed them in both VB.NET and C # with minimal code.

What I would recommend the OP is to learn how to create an installer package for it. Installing the service is as simple as executing "installutil.exe {drive] \ path \ to \ file.exe", but when you need to do something more than deploy the Windows Hello World service, it's good to know and understand how really deploy the service in a meaningful way.

Do not start a fiery war, but I "standardized" the use of WiX for all my deployment packages, outside making the old school a COM-type, since it is a load on the job to install it correctly. I really want the WiX team to develop a piece of the boot disk that allows you to put the prerequisites and the main msi in an EXE file that can be downloaded. They have version 3.5, so I will patiently wait and now use WinZip Self-Extracting executables.

+4
Feb 27 '09 at 5:10
source share

For me, and I just tried it in several ways, Visual Studio and C # were the easiest.

Visual Studio completed all the necessary setup of service plumbing for the framework, and I found that C # is very easy to learn, switching from VB6, VB.NET and C.

0
Feb 27 '09 at 3:57
source share

With Visual C #, you will find most code samples online. Combined with Visual Studio, and it’s a walk in the park to start the basic Windows service. Visual Studio also allows you to quickly create an MSI installer package.

That would be my choice.

0
Feb 27 '09 at 4:01
source share

Use a Visual Studio service type project, use C # or VB.NET.

I personally prefer C #, but overall it's easy to understand the life cycle and encode the logic at the desired stage.

Building an installer is also very simple.

0
Feb 27 '09 at 4:02
source share

A Windows service project in C # will provide you with a fully deployable service from a Visual Studio template with the click of a button. You just need to add your payload code.

0
Feb 27 '09 at 4:10
source share



All Articles