Thursday, 23 July 2015

Topshelf OSS Library – an unsung hero for windows services development

Creating a windows service itself is a hassle that includes various aspects of it like configuration, install-uninstall, recovery, start mode etc. Managing this settings is either can be achieved manually or by writing bunch of batch/command files that helps in deployment and management of windows services.
Recently, I came across an ultimate .Net OSS library that provides control almost everything related to a Windows service. The library is known as “TOPSHELF”, the contribution creds of this library goes to Travis Smith, Charles patterson and few more awesome contributors. See the well managed documentation of this project here.

How this library tool makes a difference creating a windows service? Let’s try to create a windows service:

public class TownCrier
    readonly Timer _timer;
    public TownCrier()
        _timer = new Timer(1000) {AutoReset = true};
        _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
    public void Start() { _timer.Start(); }
    public void Stop() { _timer.Stop(); }

The above code is expected to run as windows service. Now conventionally to make it a window service one must add a Window service file type then WindowsInstaller etc. Rather I’ll be using Topshelf library for hassle free setup:

Windows services made easy

public class Program
    public static void Main()
        HostFactory.Run(x =>                                 //1
            x.Service<TownCrier>(s =>                        //2
               s.ConstructUsing(name=> new TownCrier());     //3
               s.WhenStarted(tc => tc.Start());              //4
               s.WhenStopped(tc => tc.Stop());               //5
            x.RunAsLocalSystem();                            //6
            x.SetDescription("Sample Topshelf Host");        //7
            x.SetDisplayName("Stuff");                       //8
            x.SetServiceName("Stuff");                       //9
        });                                                  //10

That’s it now when I run this code my service will run as Console. Without Topshelf I would have create a separate class to run the service as console. But now I can see in few lines of code things are quite awesome. Let’s simply press F5 in visual studio to run the above code:

Running as Console service

Let’s simply press F5 in visual studio to run the above code:


Installation of window service

Simply call the generated .exe file with argument

SampleService.exe install

You will see some success log output on the command prompt:

Let’s take a look at the services window. Goto Run –> Services.msc.


And I can see my service is started and running. How cool is it..I’m sure this kinda stuff will make DevOPs guys happy. There are plenty of option that are configurable easily using this library, I can say OOS stack of .Net is coming into picture and helping developers making softwares better an better everyday.

Tuesday, 26 May 2015

Choosing your Raspberry Pi for Window 10

Microsoft has announced that there will be a windows 10 core free OS available soon for Raspberry PI. Right now it is release as preview for testing and feedback purpose. Many of you might be thinking to get your own Raspberry Pi (the cheapest computing device). In this article I’ll be discussing about the models available in the market and what you should choose for your hobby projects. Below is the image of (Early version) Raspberry PI 1 Model B+ showing the details of modules on board.

When the Raspberry Pi initial version was released it had 256 MB RAM and it was targeting the schools and for kids learning projects. This model was supporting VGA cable and 2 USB ports. Then later on 512MB version known as Raspberry PI 1 Model B+ was released with HDMI interface. But now it has became the base Module for developing IOT(Internet of things) because of it’s compact size and cheap price. The other competitor of this device is Arduino based Intel Galileo. But the prices doesn’t match even close though Galileo has it’s own benefits in terms of extensibility and performance. but Raspberry is a good place to start if you’re new to play with Circuit boards and modules.

If you’re looking to work on Embedded projects then you can choose any of the above. But you must consider the compatible OS you want to choose to run on this device. Here’s a list of recommended list that shows the comparison of various OS from big brands. Till date the Raspbian is recommended as it is optimized to run on this module.

Window 10 and Raspberry PI

Recently, the Raspberry PI next gen has been released with 1 GB ram and Quad core processor. This model comes with 4 on board USB ports making it more extensible. Seems to have much more speed and capabilities to do more with this small computing device. If you visit the above link then you can see the hardware specs and can predict what this module is capable of.
To regard the device capability, Microsoft has came up with Windows 10 Core(Currently in preview).
If you are planning to have hands-on installing and experiencing the new Windows 10 on Raspberry PI, The recommended version is Raspberry PI 2 Model B. The great thing about it’s Makers Elemen14 is that they kept the cost same as previous device. This make it more admirable.

Go ahead play, make, and share what’s your experience with this combo. Keep posted for more fun.

Tuesday, 6 January 2015

Video - Configuring and hosting vNext on Windows

This video post will guide to setup and host vNext starter application on Windows 7.
No installation no IIS. Yes we're talking about new Microsoft OSS.

Watch on youtube.

Comments/Suggestions/Feedbacks are welcome :)

Wednesday, 17 December 2014

Hosting vNext application on Linux server on Azure

In this tutorial we’ll see how to host vNext application on Linux server image running on Azure. In order to follow the steps firstly you need to have Microsoft Azure account. The supported Linux version is Ubuntu snappy.

Login to the to access your Azure account. In this article I’m using the newly launched azure portal which is having blades to support multi-view in one window. We’ll start with creating a new Linux VM on Azure and then configuring it for vNext hosting.


Step 1: Click on New and select Ubuntu


Step 2: Specify the required details for the new Linux server details.


If all details are correct and filled the Create button will get enabled. Click on Create the VM will start initializing, allocating respective resource for the Linux VM. This process will take 10-15 minutes to finish.


You’ll get something similar view of created Linux machine. If you click on Settings then Properties you can get the DomainName with other details. This domain name will be used as machine name to connect via remote client.

Step 3: Once the VM is created, Next step would be to connect with the Linux server. If you’re on windows you can use the Putty tool to connect to server console. Here are few quick steps to connect to the Linux VM.

i) Launch the Putty tool and enter the domain name of the created machine.


ii) Once you click open, if the domain name is valid a command window will open asking for Username and password. Enter the details and Linux machine will be connected and ready to receive the commands.


Step 4: Now you’re connected with the Linux server. To get the server ready now we need to install components to have the .Net core installed on the server. For Linux machine the .Net core is supplied with in MONO project from Xamarin and contributors. To check if the Mono is installed run following command.

Mono --Version 


Ofcourse the Mono is not installed on the server so we need to install it. Run following commands to install the Mono.

Note:- You can try downloading the mono-runtime with suggested command. But I’m not sure if that will give the fully featured Mono on the Linux. I haven’t tested it.

Downloading required tools:

sudo apt-get install make 
sudo apt-get install git autoconf libtool automake build-essential mono-devel 
gettext zip mono-complete unzip 

Downloading Mono and compile it. Run following command in order to get the latest Mono version ready to Install.

tar -xjvf mono-$VERSION.tar.bz2 
cd mono-$VERSION 
./ --prefix=$PREFIX 
make install 

These steps will take more than 30 minutes to downloading, compile and Install the Mono.

Now we have to install few more components like NPM, Git, KVM, and K Runtime with Kestrel server. Before download and installing these components few ssl certificated would be required to add to the certificate store of the machine. With this step the downloading of required Nugets will not take place and will give missing certificate error.

Installing certificates:

sudo certmgr -ssl -m 
sudo certmgr -ssl -m 
sudo certmgr -ssl -m 
sudo certmgr -ssl -m 

To avoid few certificated which falls under untrusted certificates run following command:

mozroots --import –sync 
Installing KVM on server 

Run following commands in order to download and install the Kvm.

curl -sSL 
| sh &amp; source ~/.kre/kvm/ 
source /home/punitganshani/.kre/kvm/ 
kvm upgrade 

Now check the Mono version again:

Mono –Version 


Note: If you don’t see the version 3.10.0 of Mono then repeat the above steps for installing the Kvm again. Because only this version is supported for Kestrel server that hosts the vNext application.

Now to verify if the K Runtime is installed Run “k”.


Installing Kestrel server

To install the Kestrel server run following commands:

sudo apt-get install npm 
npm install -g grunt-cli 
npm install kestrel-server 

Now we need a sample application so that we can test the installed components. We’ll be using the starter application from github.

Run command:

git clone 


Navigate to the following directory hierarchy:

Home -> samples -> HelloWeb

After navigating run following command:

kpm restore 
kpm build 

If there’s no error in restoring nuget packages and building the application then application is ready to Host.

Run following command to host the application in Kestrel server:

k kestrel 

You might get following error:

System.NullReferenceException: Object reference not set to an instance of an
object at
[0x00000] in &lt;filename unknown&gt;:0 at
(Microsoft.AspNet.Server.Kestrel.Networking.Libuv uv) [0x00000] in
&lt;filename unknown&gt;:0 at
Microsoft.AspNet.Server.Kestrel.KestrelThread.ThreadStart (System.Object
parameter) [0x00000] in &lt;filename unknown&gt;:0

For Web Applications, we need KestrelHttpServer which is built on libuv library.

tar -xvf libuv-v1.0.0-rc1.tar.gz 
cd libuv-v1.0.0-rc1/ 
./ -f make -Duv_library=shared_library 
make -C out 
sudo cp out/Debug/ /usr/lib/ 
sudo ln -s /usr/lib/ 

Now runt the command again:

k kestrel 

It should say “Started” at this time.


The application is hosted at localhost:5004 (default port for Kestrel server). It can be changed from project.config file.


Exposing hosted website:

Now to expose the hosted site to external world last thing is to add an Endpoint on your Linux machine via Azure portal. To add the Endpoint go back to the Azure portal select the Linux VM -> Click Settings -> Endpoints -> Add.


Fill the details. If all the End points details are valid, click Ok. Now your machine is ready to show the hosted vNext starter application to external world.

Now open browser on local machine. Goto http://<your domain name>


… And you’ll your hosted started application.


Wednesday, 10 December 2014

Building Hosting vNext app on Macbook

In previous post we have learned how to start using the new 5 hosting environment named “K” without using IIS. In this article we’ll learn how to start developing web application on Macbook. As Microsoft announced the OSS of .Net technologies mean one should be able to contribute/create/host apps across platform including Linux, Max and Windows.


The overview of developing and deploying cross platform of web app really exciting thing coming with vNext. One can develop and deploy on any platform they want. I tried to create a picture representing Idea of cross platform develop/deploy.


In above picture the interesting part is Azure gives you an opportunity to create Linux based Virtual Machine. And visual studio enables the deployment and hosting your Web app directly from new Visual Studio 2015. Isn’t it interesting?

Get started

Let start with instructions of how to create/run on a Mac machine. Though all the steps are already specified on github page but to give you more clearer picture of steps with screenshot would definitely help one to step ahead having hands on.

To install KVM (Kruntime** Version Manager) and the correct version of Mono on OS X using Homebrew follow the following steps:

**Note: Kruntime is code that bootstraps and runs an ASP.NET vNext application. This includes things like the compilation system, SDK tools, and the native CLR hosts.

Step 1 -

Install Homebrew if it is not already installed.

Run following command on terminal to install Brew via terminal:

ruby -e "$(curl -fsSL"


Here you need to press Return key. You’ll see following output:


Here you need to insert the password of user you logged-in with.(Make sure user have Administrative rights). Also the cursor will not show you anything when you type password so just type and press Return key and the homebrew will be installed on your machine.


Step 2:
Run command brew tap aspnet/k to tap the ASP.NET vNext related git repositories. If you had already tapped the repo for previous releases, run brew untap aspnet/k to delete the old commands and tap again to get the updated brew scripts.


Step 3:
Run command brew install kvm to install KVM. This also automatically install the latest KRE package from feed.


Step 4:
Run command source on your terminal if your terminal cannot understand kvm.

Step 5:

Run command kvm upgrade. This will install the latest version of Kruntime. Now you’re ready with your Kruntime to host and run vNext apps. Now to start with creating applications you would need an Editor. In this tutorial I’m going to use the Sublime Text 3. Download and install if it’s not already install on your macbook.

Step 6: Setting up and C# plugin for Sublime

Omnisharp have developed many plugins for several famous editors for various platforms. So in order to setup an development environment I have to install the Omnisharp plugin for Sublime. Follow the instructions to make the sublime and C# development ready with similar experience as we have in Visual studio.


Step 7: Getting a solution ready in Sublime text editor.

If you want to start with creating a fresh .Net solution then follow the blogs post here. The post will show you how to create a sublime project and convert it into the vNext project.

In this post I’ll just use the sample solutions which are already there on home branch to save some time for me and you. Install Github for OSx if it’s not already installed. Now clone the home branch from the terminal by running the following command:


Open the project Hello Mvc in the Sublime to check if your color scheme, Language and Intellisense settings are done correctly.


Step 8: Building and Restoring the nuget packages of downloaded samples

Run kpm restore command from terminal. This will restore the nuget packages required to build the project. Make sure you navigate to the directory where the project resides before running the command.


Now your first sample web is ready to host and run on Mac.

Step 9: Hosting and running sample web

Run command k kestrel. This command will build and host the web app on mac. The default port where the application will be running is http://localhost:5004. This is configuration from project.config file. I’ll be writing another post about the new vNext project structure and configurations.



If you see any build errors in code you can open the web application in Sublime editor and fix the build errors if there are compilation errors. To build the from web first change the setting in the Project menu item. Change the Build System to


Fix the compilation errors and build the project using command + B.


That’s all. Happy coding!!!

If you have any questions/doubts or feedback about the article feel free to drop comments below.