What is the easiest language to build Windows services in?
Easiest in this case would be defined as least amount of code, and lowest point of entry into the language.
Let's create a Windows Service in C# using Visual Studio. Open Visual Studio, go to File > New and select Project. Now select a new project from the Dialog box and select “Window Service” and click on the OK button.
To create a Windows Service application In the Properties window, set the ServiceName property for your service. The value of the ServiceName property must always match the name recorded in the installer classes. If you change this property, you must update the ServiceName property of installer classes as well.
The closest Microsoft programming language to both is C#. For most developers and most apps, we think C# is the easiest and fastest language to learn and use, so this article's info and walkthroughs focus on that language. To learn more about C#, see the following: Create your first UWP app using C# or Visual Basic.
At the risk of stating the obvious, if you have any C/C++/Java background, I think C# offers you the lowest point of entry.
Assuming you're using Visual Studio 2008, you can follow these steps:
false
to prevent events from being written by default to the Application event log (Note: I'm not saying you shouldn't log service events; I just prefer writing to my own event log instead of the Application log - see below)true
if you want to handle system shutdownstrue
, you'll want to override the OnShutdown method as well. I've created an example below illustrating the use of these functions.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 build your project to get your Windows service executable. To install your service, open the Visual Studio 2008 command prompt, and navigate to the Debug or Release directory where your executable is. At the command prompt, type the following: InstallUtil ServiceExample.exe. This will install your service on the local machine. To uninstall it, type the following at the command prompt: InstallUtil /u ServiceExample.exe
As long as your service is not running, you can make changes to your service and re-build, i.e., you do not have to uninstall your service to make changes to it. However, you will be unable to overwrite the executable with your fixes and enhancements as long as 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); } }
Once you run your service with these changes, you can look at the ServiceExample event log in the Event Viewer and see the messages logged there.
Hope this helps.
EDIT: If you prefer to use the Application event log for your event logging instead of a custom one, simply make no changes to the ProjectInstaller.cs file. In addition, leave out the line that sets the Log property of the EventLog in the ServiceExample constructor. When you run your service, your log messages will appear in the Application event log.
I agree with everyone that has responded elsewhere, but I would say don't focus too much on the actual language, as long as you're working in the .NET framework, and there's a starter project for you, you're good to go. I've done several "Windows services" in the past, and developed them both in VB.NET and C#, with minimal code.
What I would recommend the OP to do is to learn how to build the installer package for it. Installing the service is as easy as executing "installutil.exe {drive}\path\to\file.exe", but when you have to do anything larger than deploying a "hello world" Windows service, it's good to know and understand how do deploy the service in a meaningful way.
Not to start a flame war, but I've "standardized" on using WiX for all my deployment packages, outside of doing old-school COM interop type stuff, since that's a brunt of work to get to install correctly. I'm anxious for the WiX team to develop the bootstrapper piece that allows you to put the prerequisites and the main msi into an EXE file which can be downloaded. They have it slated for 3.5, so I'll patiently wait, and use WinZip Self-Extracting executables for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With