Building services with the .NET framework Cont’d

Posted by Allan Rwakatungu on Geeks with Blogs See other posts from Geeks with Blogs or by Allan Rwakatungu
Published on Tue, 08 Jun 2010 15:19:11 GMT Indexed on 2010/06/08 16:23 UTC
Read the original article Hit count: 828

Filed under:

In my previous blog I wrote an introductory post on services and how you can build services using the .NET frameworks Windows Communication Foundation (WCF)
In this post I will show how to develop a real world application using WCF

The problem


During the last meeting we realized developers in Uganda are not so cool – they don’t use twitter so may not get the latest news and updates from the technology world. We also noticed they mostly use kabiriti phones (jokes).

With their kabiriti phones they are unable to access the twitter web client or alternative twitter mobile clients like tweetdeck , twirl or tweetie.

However, the kabiriti phones support SMS (Yeeeeeeei).

So what we going to do to make these developers cool and keep them updated is by enabling them to receive tweets via SMS.

We shall also enable them to develop their own applications that can extend this functionality

Analysis

Thanks to services and open API’s solving our problem is going to be easy.

 1. To get tweets we can use the twitter service for FREE

2. To send SMS we shall use www.clickatell.com/ as they can send SMS to any country in the world. Besides we could not find any local service that offers API's for sending SMS :(.

3. To enable developers to integrate with our application so that they can extend it and build even cooler applications we use WCF. In addittion , because connectivity might be an issue we decided to use WCF because if has a inbuilt queing features.
We also choose WCF because this is a post about .NET and WCF :).

The Code

Accessing the tweets

To consume twitters REST API we shall use the WCF REST starter kit.
Like it name indicates , the REST starter kit is a set of .NET framework classes that enable developers to create and access REST style services ( like the twitter service).


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Http;

using System.Net;

using System.Xml.Linq;

 

namespace UG.Demo

{

    public class TwitterService

    {

        public IList<TwitterStatus> SomeMethodName()

        {

            //Connect to the twitter service (HttpClient is part of the REST startkit classes)

            HttpClient cl = new HttpClient("http://api.twitter.com/1/statuses/friends_timeline.xml");

            //Supply your basic authentication credentials

            cl.TransportSettings.Credentials = new NetworkCredential("ourusername", "ourpassword");

            //issue an http

            HttpResponseMessage resp = cl.Get();

            //ensure we got reponse 200

            resp.EnsureStatusIsSuccessful();

            //use XLinq to parse the REST XML

            var statuses = from r in resp.Content.ReadAsXElement().Descendants("status")

                           select new TwitterStatus

                           {

                               User = r.Element("user").Element("screen_name").Value,

                               Status = r.Element("text").Value

                           };

            return statuses.ToList();

        }

    }

    public class TwitterStatus

    {

        public string User { get; set; }

        public string Status { get; set; }

    }

}

 Sending SMS

public class SMSService

    {

        public void Send(string phone, string message)

        {

           

            HttpClient cl1 = new HttpClient();

             //the clickatell XML format for sending SMS

            string xml = String.Format("<clickAPI><sendMsg><api_id>3239621</api_id><user>ourusername</user><password>ourpassword</password><to>{0}</to><text>{1}</text></sendMsg></clickAPI>",phone,message);

            //Post form data

            HttpUrlEncodedForm form = new HttpUrlEncodedForm();

            form.Add("data", xml);

            System.Net.ServicePointManager.Expect100Continue = false;

            string uri = @"http://api.clickatell.com/xml/xml";

            HttpResponseMessage resp = cl1.Post(uri, form.CreateHttpContent());

            resp.EnsureStatusIsSuccessful();

        }

    }

© Geeks with Blogs or respective owner