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).
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
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
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
MicrosoftInternetExplorer4
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
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();
}
}