3 lines of Node.JS code to run a Web Server on ServiceFabric

Since Service Fabric got publically released we saw many enthusiasts trying creating their apps with this. One of the proven strong case study is having a website hosted on Service Fabric.

If one goes through the documentation of Service Fabric he/she will find the tutorial on OWIN hosted webapp at https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-communication-webapi/. This is pretty descriptive and explains how quickly you can host a website on Service fabric.

I started this blog taking reference of this tutorial. The intent was to reduce the code and try to come up with a story around this. This was the time when I thought to see if we can figure out something with Node.JS. My early experience with Node.Js was pretty exciting wherein we created a Azure Mobile Service pinger to Service fabric Cluster. http://nabaruns.blogspot.com/2015/05/azure-mobile-service-service-fabric.html. The only challenge was how to call Node.JS script from C#.

With a little bit of research I found that this issue has been solved by Tomasz Janczuk. Acknowledging Tomasz let me now touch a little bit of Edge.JS.

Edge.JS GIT: https://github.com/tjanczuk/edge

Pre-requisite of this exercise is you have Node.JS and Service Fabric installed in your machine.

Step By Step : How to create a web host from Service Fabric

1. In my example I have created a Stateful Service Fabric Service. Stateful is just by choice as I plan to explore some more stuff with Node.JS and at a later stage have a stateful story around that. You can start with stateless or actor services too

2. Install the Nuget package of Edge.js from NPM (Tools -> Nuget Package Manager)
3. Now let's write our 3 line code for creating the webserver

 public static void Main(string[] args)
                using (FabricRuntime fabricRuntime = FabricRuntime.Create())
                    // This is the name of the ServiceType that is registered with FabricRuntime.
                    // This name must match the name defined in the ServiceManifest. If you change
                    // this name, please change the name of the ServiceType in the ServiceManifest.

                    fabricRuntime.RegisterServiceType("WebHostStaefulSFType", typeof(WebHostStaefulSF));
                    ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(WebHostStaefulSF).Name);
                    CallWebServer("Thanks for creating this server").Wait();
            catch (Exception e)
private async static Task CallWebServer(string greeting)
            const string jSFunction = "return function createServer (data, callback){ var http = require(\"http\");  var server = http.createServer(function(request, response) { response.write(\"Hello World!\"); response.end();});server.listen(8081);}";

            var func = Edge.Func(jSFunction);
            WriteLine(await func(greeting));

That's it. That is what we need to spawn a http web server using node.js

As per my understanding, from Service Fabric point of view it does not care how the web server is hosted. Here it just considers the Primary replica is ready or not. Once ready it executes the code.

It will be great to know other views on this.



  1. Thanks Admin, I just begin my career in sever side web application development for that I complete the Node JS training but I wish to explore more in that, truly your blog provide some useful information and I would like to share your blog in my friends circle so keep sharing.
    Node JS training in chennai

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

  2. Hai Author Good Information that i found here,do not stop sharing and Please keep updating us..... Thanks. Hire Nodejs Developers


Post a Comment

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module