Click here to Skip to main content
15,891,847 members
Articles / Programming Languages / C# 5.0
Tip/Trick

EasyService4Net - Create a Windows Service in 100 Seconds or Less

Rate me:
Please Sign up or sign in to vote.
4.77/5 (17 votes)
9 Apr 2016MIT2 min read 26.3K   328   38   7
How to create a program that can run as console or as Windows service easily, and install it without using installutil

Introduction

If you ever wanted to created a new Windows service that would be able to run as console as well you know it may be really annoying, and can take much time.

In this tip, I will explain how it can be done very easily.

Using the Code

Open a new Visual Studio console application and add the EasyService4Net nuget package to it:

Image 1

In Program.cs file, add usings:

C#
using System.IO;
using EasyService4Net;

Write the following code in the Main function:

C#
static void Main(string[] args)
{
    var easyService = new EasyService();
    easyService.OnServiceStarted += 
                () => File.WriteAllText("C:\\exampleFile.txt", "Hello World!");

    easyService.Run(args);
}

Here, you can write the code that you want to run when the service starts. In this example, it will write "Hello World" in a file.

Now you need to add two empty classes to the project (without it, the Windows service won't work!):

C#
using EasyService4Net.ServiceInternals;
C#
public class EasyServiceInstaller : ProjectInstaller { }
public class EasyServiceService : InternalService { }

And add two references to the project:

  1. System.Configuration.Install
  2. System.ServiceProcess

That's it, now you can install the service as Windows service:

Run the cmd as administrator (In order to install Windows service, you must be administrator), go to the folder where the compiled code is found and write (if your EXE is called MyEasyService):

MyEasyService.exe -install

Image 2

Your Windows service is running:

You can enter services.msc and see it:

Image 3

If you open the file "C:\\example.txt", you will see "Hello World" written in it.

If you want to uninstall the service, all you need to do is this:

MyEasyService.exe -uninstall

Pay attention that you can also run this program as a console (as administrator):

Image 4

Notes

  1. GitHub of EasyService4Net - https://github.com/TheCodeCleaner/EasyService4Net
  2. If you don't want to be dependent on the EasyService4Net nuget, you can simply take its code to your project, and manage it by yourself.
  3. StackOverflow answer on how to make applications run as administrator by default (very useful)- http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7
  4. I hope you like this tip and will use it for your Windows services from now.
  5. Feel free to write any suggestions or issues you have found.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralYep....TopShelf Pin
Tim Schwallie11-Apr-16 7:52
Tim Schwallie11-Apr-16 7:52 
GeneralRe: Yep....TopShelf Pin
King David Consulting LLC11-Apr-16 7:58
professionalKing David Consulting LLC11-Apr-16 7:58 
QuestionNice Pin
Sacha Barber5-Jul-15 21:51
Sacha Barber5-Jul-15 21:51 
We use "TopShelf" for this where I work, same idea. I like what you have done here, have a 5

AnswerRe: Nice Pin
Alon Lek6-Jul-15 6:02
Alon Lek6-Jul-15 6:02 
GeneralRe: Nice Pin
Southmountain6-Jul-15 10:26
Southmountain6-Jul-15 10:26 
GeneralRe: Nice Pin
Garth J Lancaster9-Apr-16 21:12
professionalGarth J Lancaster9-Apr-16 21:12 
GeneralGreat! Pin
Refactor Man5-Jul-15 8:05
Refactor Man5-Jul-15 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.