Click here to Skip to main content
13,792,947 members
Click here to Skip to main content
Add your own
alternative version

Stats

152.2K views
9.2K downloads
78 bookmarked
Posted 10 Apr 2015
Licenced MIT

MicroService4Net - Create Micro Services Easily with C#

, 14 Apr 2016
Rate this:
Please Sign up or sign in to vote.
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.

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

License

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

Share

About the Author

Code Cleaner
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionDive-into-Microservices-Architecture-Part-I Pin
Aydin Homay1-Nov-18 1:33
memberAydin Homay1-Nov-18 1:33 
Questionhow do you add component/s in real-time? Pin
orlandocanada15-Jun-17 4:56
memberorlandocanada15-Jun-17 4:56 
AnswerRe: how do you add component/s in real-time? Pin
Code Cleaner17-Jun-17 11:45
memberCode Cleaner17-Jun-17 11:45 
Questionhttppost does not seem to work Pin
goldust24-Apr-17 6:41
membergoldust24-Apr-17 6:41 
AnswerRe: httppost does not seem to work Pin
Code Cleaner29-May-17 11:50
memberCode Cleaner29-May-17 11:50 
GeneralRe: httppost does not seem to work Pin
Sylvain Lucas7-Sep-17 4:34
memberSylvain Lucas7-Sep-17 4:34 
Questioncan the sampke work on win7 ? Pin
bugal23-Nov-16 2:12
memberbugal23-Nov-16 2:12 
AnswerRe: can the sampke work on win7 ? Pin
Code Cleaner26-Nov-16 10:26
memberCode Cleaner26-Nov-16 10:26 
QuestionHow would you set up Dependency Injection on ExampleController.cs? Pin
RicoZaid4-Nov-16 11:10
memberRicoZaid4-Nov-16 11:10 
AnswerRe: How would you set up Dependency Injection on ExampleController.cs? Pin
Code Cleaner14-Nov-16 10:52
memberCode Cleaner14-Nov-16 10:52 
QuestionException starting the console application Pin
Member 127222876-Sep-16 15:40
memberMember 127222876-Sep-16 15:40 
AnswerRe: Exception starting the console application Pin
Code Cleaner9-Sep-16 1:15
memberCode Cleaner9-Sep-16 1:15 
QuestionI am trying this code in a single window service which have to invoke API Pin
sam.callme77714-Aug-16 5:16
membersam.callme77714-Aug-16 5:16 
AnswerRe: I am trying this code in a single window service which have to invoke API Pin
Code Cleaner9-Sep-16 1:22
memberCode Cleaner9-Sep-16 1:22 
Question[My vote of 1] How is this a microservice Pin
Vander Wunderbar17-Apr-16 9:19
memberVander Wunderbar17-Apr-16 9:19 
AnswerRe: [My vote of 1] How is this a microservice Pin
Code Cleaner18-Apr-16 20:35
memberCode Cleaner18-Apr-16 20:35 
GeneralRe: [My vote of 1] How is this a microservice Pin
Vander Wunderbar20-Apr-16 12:42
memberVander Wunderbar20-Apr-16 12:42 
GeneralRe: [My vote of 1] How is this a microservice Pin
Code Cleaner21-Apr-16 6:09
memberCode Cleaner21-Apr-16 6:09 
GeneralRe: [My vote of 1] How is this a microservice Pin
Vander Wunderbar21-Apr-16 13:34
memberVander Wunderbar21-Apr-16 13:34 
QuestionHow can i create a MicroService which is called from WCF RESTFul API ? Pin
aniruddha553214-Mar-16 0:59
memberaniruddha553214-Mar-16 0:59 
AnswerRe: How can i create a MicroService which is called from WCF RESTFul API ? Pin
Code Cleaner16-Mar-16 10:17
memberCode Cleaner16-Mar-16 10:17 
PraiseVery quick and easy way to build a Windows Service Pin
mrrobot2329-Jan-16 8:34
membermrrobot2329-Jan-16 8:34 
QuestionGreat tip, but how this is a microservice Pin
Ashish Sheth2-Sep-15 3:00
memberAshish Sheth2-Sep-15 3:00 
AnswerRe: Great tip, but how this is a microservice Pin
Ray_Cheng3-Feb-16 7:53
memberRay_Cheng3-Feb-16 7:53 
GeneralRe: Great tip, but how this is a microservice Pin
TheGreatAndPowerfulOz9-Apr-16 6:45
memberTheGreatAndPowerfulOz9-Apr-16 6:45 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181207.3 | Last Updated 14 Apr 2016
Article Copyright 2015 by Code Cleaner
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid