MicroService4Net - Create Micro Services Easily with C# - CodeProject

:

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.

Install the dependencies:

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();
    microService.Run(args);
}

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

using System.Web.Http;
public class ExampleController : ApiController
{
    [Route("Example")]
    public string GetExample()
    {
        return "Example";
    }
}

Now, you can just run it as console application:

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

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

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

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

And you can check it with a browser:

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