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

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 255.6K   11.4K   80   34
Create a simple self hosted Web API service that runs as console and as service in just 2 minutes

Introduction

"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 (http://en.wikipedia.org/wiki/Microservices)

Background

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:

C#
using MicroService4Net;

Write the following code in the Main function:

C#
static void Main(string[] args)
{
    var microService = new MicroService();
    microService.Run(args);
}

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

C#
using System.Web.Http;
C#
public class ExampleController : ApiController
{
    [Route("Example")]
    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:

C#
static void Main(string[] args)
{
    var microService = new MicroService(1234);
    microService.Run(args);
}

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!):

C#
using MicroService4Net.ServiceInternals;
C#
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:

MyMicroService.exe

Updates

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

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

 
PraiseVery quick and easy way to build a Windows Service Pin
mrrobot2329-Jan-16 7:34
mrrobot2329-Jan-16 7:34 
QuestionGreat tip, but how this is a microservice Pin
Ashish Sheth2-Sep-15 2:00
Ashish Sheth2-Sep-15 2:00 
AnswerRe: Great tip, but how this is a microservice Pin
Ray_Cheng3-Feb-16 6:53
Ray_Cheng3-Feb-16 6:53 
GeneralRe: Great tip, but how this is a microservice Pin
TheGreatAndPowerfulOz9-Apr-16 5:45
TheGreatAndPowerfulOz9-Apr-16 5:45 
QuestionInteract With Hardware Pin
tarunsrivastava18-Apr-15 1:55
professionaltarunsrivastava18-Apr-15 1:55 
AnswerRe: Interact With Hardware Pin
TheGreatAndPowerfulOz9-Apr-16 5:46
TheGreatAndPowerfulOz9-Apr-16 5:46 
QuestionOWIN Pin
codefabricator14-Apr-15 6:18
codefabricator14-Apr-15 6:18 
AnswerRe: OWIN Pin
Alon Lek14-Apr-15 9:34
Alon Lek14-Apr-15 9:34 
AnswerRe: OWIN Pin
TheGreatAndPowerfulOz9-Apr-16 6:04
TheGreatAndPowerfulOz9-Apr-16 6:04 

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.