Click here to Skip to main content
15,354,474 members
Articles / Programming Languages / C# 5.0
Posted 10 Apr 2015


80 bookmarked

MicroService4Net - Create Micro Services Easily with C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (23 votes)
14 Apr 2016MIT3 min read
Create a simple self hosted Web API service that runs as console and as service in just 2 minutes


"Microservices is a software architecture design pattern in which complex applications are composed of small, independent processes communicating with each other using language-agnostic APIs. These services are small, highly decoupled and focus on doing a small task."

From Wikipedia (


In the last couple of years, microservices became a common software architecture in many complex systems.

In this tip, I want to introduce a library that I wrote (MicroService4Net) that makes the creation of such services very easy for C# programmers.

So, if you follow my instructions, you will get an HTTP server that reacts to HTTP requests and returns json responses (very roughly speaking).

You can run it as a Windows service or as a console application (you don't need to choose only one of them when you open your new project).

And you don't need IIS for this (everything is self hosted).

Using the Code

You can download the source code from here.

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

Pay attention to use the Highest Dependency behavior.

Image 1

Install the dependencies:

Image 2

In Program.cs file, add this using:

using MicroService4Net;

Write the following code in the Main function:

static void Main(string[] args)
    var microService = new MicroService();

Add a new class, for your controller (ExampleController for example):

using System.Web.Http;
public class ExampleController : ApiController
    public string GetExample()
        return "Example";

Now, you can just run it as console application:

Image 3

Open a web browser and browse to http://localhost:8080/Example:

Image 4

The port is 8080 by default. However, if you want to change the port, simply do the following in the Main function:

static void Main(string[] args)
    var microService = new MicroService(1234);

Now, it will run on 1234 port.

Please note that if you want this to run on port 80, you should run the program as administrator.

Now, you can write whatever service you want with this, if you are familiar with web API controllers, it is exactly the same. The time that it takes to create a new service is pretty low, so you can concentrate on your real logic.

Till now, we run it as console application, however if you want it to run as Windows service, it is also possible!

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

using MicroService4Net.ServiceInternals;
public class MicroServiceInstaller : ProjectInstaller { }
public class MicroServiceService : 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 MyMicroService):

MyMicroService.exe -install

Image 5

Now you have a Windows service running that will start automatically on every system boot.

And you can check it with a browser:

Image 6

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

MyMicroService.exe -uninstall

You even don't need to stop the Windows service.

And of course, if you want to run it just as console:



Version 2 of MicroService4Net is released. This version is using Owin instead of ASP.NET Web Api Self Host, as codefabricator suggested . The api has not changed.

Cors is now enabled by default. if you don't want it to be enabled you can create the MicroService is such a way:

var microService = new MicroService(useCors: false);

What's Next?

My code is actually open source and can be found in the Useful Links section.

If you want some explanations about the code, I will be glad to write another article with explanations.

Feel free to write any suggestions or issues you have found.

Useful Links


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


About the Author

Alon Lek
Israel Israel
No Biography provided

Comments and Discussions

Questionasp.core Pin
franckgar9-Apr-21 3:43
Memberfranckgar9-Apr-21 3:43 
QuestionException in ExampleController class Pin
itais1701E29-Sep-20 2:03
Memberitais1701E29-Sep-20 2:03 
QuestionMicrosoft.Owin dll problem Pin
Member 143549724-May-19 4:28
MemberMember 143549724-May-19 4:28 
Questionhttp://localhost:8080/Example doesn't work. WHY? Pin
Member 1417989812-Mar-19 8:45
MemberMember 1417989812-Mar-19 8:45 
QuestionDive-into-Microservices-Architecture-Part-I Pin
Aydin Homay1-Nov-18 0:33
MemberAydin Homay1-Nov-18 0:33 
Questionhow do you add component/s in real-time? Pin
orlandocanada15-Jun-17 3:56
Memberorlandocanada15-Jun-17 3:56 
AnswerRe: how do you add component/s in real-time? Pin
Alon Lek17-Jun-17 10:45
MemberAlon Lek17-Jun-17 10:45 
Questionhttppost does not seem to work Pin
goldust24-Apr-17 5:41
Membergoldust24-Apr-17 5:41 
AnswerRe: httppost does not seem to work Pin
Alon Lek29-May-17 10:50
MemberAlon Lek29-May-17 10:50 
GeneralRe: httppost does not seem to work Pin
Sylvain Lucas7-Sep-17 3:34
MemberSylvain Lucas7-Sep-17 3:34 
Questioncan the sampke work on win7 ? Pin
bugal23-Nov-16 1:12
Memberbugal23-Nov-16 1:12 
AnswerRe: can the sampke work on win7 ? Pin
Alon Lek26-Nov-16 9:26
MemberAlon Lek26-Nov-16 9:26 
QuestionHow would you set up Dependency Injection on ExampleController.cs? Pin
RicoZaid4-Nov-16 10:10
MemberRicoZaid4-Nov-16 10:10 
AnswerRe: How would you set up Dependency Injection on ExampleController.cs? Pin
Alon Lek14-Nov-16 9:52
MemberAlon Lek14-Nov-16 9:52 
QuestionException starting the console application Pin
Member 127222876-Sep-16 14:40
MemberMember 127222876-Sep-16 14:40 
AnswerRe: Exception starting the console application Pin
Alon Lek9-Sep-16 0:15
MemberAlon Lek9-Sep-16 0:15 
QuestionI am trying this code in a single window service which have to invoke API Pin
sam.callme77714-Aug-16 4:16
Membersam.callme77714-Aug-16 4:16 
AnswerRe: I am trying this code in a single window service which have to invoke API Pin
Alon Lek9-Sep-16 0:22
MemberAlon Lek9-Sep-16 0:22 
Question[My vote of 1] How is this a microservice Pin
Vander Wunderbar17-Apr-16 8:19
MemberVander Wunderbar17-Apr-16 8:19 
AnswerRe: [My vote of 1] How is this a microservice Pin
Alon Lek18-Apr-16 19:35
MemberAlon Lek18-Apr-16 19:35 
GeneralRe: [My vote of 1] How is this a microservice Pin
Vander Wunderbar20-Apr-16 11:42
MemberVander Wunderbar20-Apr-16 11:42 
GeneralRe: [My vote of 1] How is this a microservice Pin
Alon Lek21-Apr-16 5:09
MemberAlon Lek21-Apr-16 5:09 
GeneralRe: [My vote of 1] How is this a microservice Pin
Vander Wunderbar21-Apr-16 12:34
MemberVander Wunderbar21-Apr-16 12:34 
QuestionHow can i create a MicroService which is called from WCF RESTFul API ? Pin
aniruddha553213-Mar-16 23:59
Memberaniruddha553213-Mar-16 23:59 
AnswerRe: How can i create a MicroService which is called from WCF RESTFul API ? Pin
Alon Lek16-Mar-16 9:17
MemberAlon Lek16-Mar-16 9:17 

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.