Playing with NextGen Distributed computing - Service Fabric

Really excited to share the Service Fabric work with all. After a long wait of around 2 years, finally the moment has arrived where we can talk about Service Fabric on a public forum.

What is Service Fabric

For those who are new to Service Fabric. This is a distributed computing framework which is getting released while I am writing this blog.

Details at:
http://azure.microsoft.com/en-us/documentation/articles/service-fabric-get-started/

Owin-Self Host Key Value Sample with Facebook Analysis

In this example we are going to self host using Owin. At the end of the sample we will query the api controller and verify the call

Pre-requisites 

We need to install Microsoft ASP.NET WebAPI 2.2 OWIN and Microsoft.Owin.Hosting
 
 

Details 


We will be using KVS Service Fabric Sample for this.

  1. Create a KeyValue Min project
 
 

2. Replace the Start() in Service.cs with this code
private void Start()
        {
            // TODO: start listener
            this.StartListening();
            Console.WriteLine("Started Listener: ");
            this.isListening = true;
            this.cancellationTokenSource = new CancellationTokenSource();
            Task.Factory.StartNew(() =>
            {
                while (this.cancellationTokenSource != null && !this.cancellationTokenSource.IsCancellationRequested)
                {
                    PartitionAccessStatus writeStatus = this.partition.WriteStatus;
                    if (writeStatus == PartitionAccessStatus.Granted)
                   {
                        this.StartOwinHost();
                        Task.Delay(Properties.Settings.Default.WriteStatusTimeInInterval).Wait();
                    }
                    else
                    {
                        Task.Delay(Properties.Settings.Default.WriteStatusTimeInInterval).Wait();
                    }
                }
            });
       
 
Here we are call a StartOwinHost() which will use Microsoft.Owin.Hosting to create ApiController.
 

3. Start Owin Host in StartOwinHost()

private void StartOwinHost()
        {
            //Starting a listener using owinhost
            string baseAddress = Properties.Settings.Default.DefaultLocal;
 
            // Start OWIN host
            using (WebApp.Start<Startup>(url: baseAddress))
            {
//We will add code here later
            }
            Console.ReadLine();
       
Here  Properties.Settings.Default.DefaultLocal = "http://localhost:8000/". We will add the verification part to this code later
 
4. Now we will create an interface for our API Controller, we will name it as IOwinService
 public interface IOwinService
    {
        [OperationContract]
        [WebInvoke(Method="GET",
            UriTemplate="",           
            ResponseFormat=WebMessageFormat.Json)]
        string GetOwinMessages();
        [OperationContract]
        [WebInvoke(Method = "POST",
            UriTemplate = "",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        void AddOwinMessage(string keyword);
   

5. The last thing is to add a controller for our web api, we name this as ValuesController. Please Note here the GET API returns a string array.  
public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "azure","Service Fabric supports azure" };
        }
    }     
6. The last this is to verify the API. We will add the verification code to our StartOwinHost()
 private void StartOwinHost()
        {
            //Starting a listener using owinhost
            string baseAddress = Properties.Settings.Default.DefaultLocal; 
            // Start OWIN host
            using (WebApp.Start<Startup>(url: baseAddress))
            {
                // Create HttpCient and make a request to api/values
                HttpClient client = new HttpClient();
                var response = client.GetAsync(baseAddress + "api/values").Result;
                Console.WriteLine(response);
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            }
            Console.ReadLine();
        }
 
7. Now let's execute this code. Following snapshot is from primary replica where in there was a GET API request.

 

Using Facebook Graph as analytics using key value store and show the analytics in OWIN GET API

 
  1. First we need to get the list of facebook posts corresponding to the query 
public async Task<List<Message>> GetMessages(string query)
        {
            List<Message> facebookMessages = new List<Message>();
            string getFacebookMessage = "https://graph.facebook.com/search?q='" + query + "'&type=post&access_token=<accesstoken>"; 
            using (HttpClient facebookClient = new HttpClient())
            using (HttpResponseMessage facebookResponse = facebookClient.GetAsync(getFacebookMessage).Result)
            {
 
                facebookResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                facebookResponse.EnsureSuccessStatusCode();
                string responseBody = await facebookResponse.Content.ReadAsStringAsync();
 
                //Add the value to key value store
                JObject json = JObject.Parse(responseBody);
                JArray array = (JArray)json["data"];
 
                foreach (var item in array)
                {
                    if (item["message"] != null)
                    {
                        Message fbMsg = new Message();
                        fbMsg.Key = item["id"].ToString();
                        fbMsg.Messages = item["message"].ToString();
                        facebookMessages.Add(fbMsg);
                    }
                }
                return facebookMessages;
            }
       
  1. We need to add these messages to our key value store
Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        while (this.cancellationTokenSource != null && !this.cancellationTokenSource.IsCancellationRequested)
                        {
                            PartitionAccessStatus writeStatus = this.partition.WriteStatus;
                            if (writeStatus == PartitionAccessStatus.Granted)
                            {
                                FacebookAnaltics fbObj = new FacebookAnaltics();
                                var messages = fbObj.GetMessages("Microsoft").Result;
                                using (Transaction tx = this.CreateTransaction())
                                {
                                    foreach (var fbMsg in messages)
                                    {
                                        this.Add(tx, fbMsg.Key, Encoding.ASCII.GetBytes(fbMsg.Messages));
                                    }
                                    tx.CommitAsync().Wait();
                                }
                                Task.Delay(Properties.Settings.Default.WaitTimeStatusInMin * 60 * 1000).Wait();
                            }
                            else
                            {
                                Task.Delay(Properties.Settings.Default.WriteStatusTimeInInterval).Wait();
                            }
                        }
                    }
                });

 

Comments

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module