Home Contact

Allan Rwakatungu's blog

Developer and Enterprise Architect

News

Twitter












Archives

Post Categories

Image Galleries

Syndication:

Windows Azure - Custom Diagnostics and Logging with Loggly

Let me state the obvious, for any application you develop you need to log information, errors, exceptions etc that can help you and others know what’s going on with your application
When you create a new Windows Azure application using Visual Studio you will find the following snippet in your configuration file
<system.diagnostics>
    <trace>
      <listeners>
        <addtype="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          name="AzureDiagnostics">
          <filtertype="" />
        </add>
      </listeners>
    </trace>
 </system.diagnostics>
 
You have diagnostic’s right out of the box , and you can write code as below
Trace.TraceInformation(“Information to log”);
 
Diagnosticsare then logged to Azure blob storage.
You can now access and analyse your logs using tools like Cerebranta Azure Diagnostics manager
However, you might require to not use the default Azure diagnostics
For example , you might choose to use a service like Loggly to log and analyse your logs.
You also get added features like alerts which is why I decided to use them
To setup logging with loggy is easy
Sign up for a loggly account from their website
Download the .NET loggly API driver at http://wiki.loggly.com/dotnetlogging
You can then implement the TraceListener abstract class
public class LogglyTraceListener:TraceListener
    {
        public override void Write(string message)
        {
            this.WriteLine(message);
        }
        public override void WriteLine(string message)
        {
            ILogger logger = new Loggly.Logger("APIKeyHere");
            logger.LogAsync(message);
        }
    }
 
In you configuration file , change diagnostics to use your new trace listener
   <trace>
      <listeners>
        <addtype="LogglyTraceListener, Logging"name="Loggly">
          <filtertype="" />
        </add>
      </listeners>
    </trace>
 
Now when you write Trace logs they should show up in loggly ready for you to analyse
loggly
Happy coding
 
 

 



The lean startup movement - Video

I just had to share this.

I wil blog about my experience with customer development and lean startups in 6 months

 


Watch live video from Startup Lessons Learned on Justin.tv



3 good reasons why am in love with the cloud - Windows Azure Cloud

Yes, am in love. One day am ranting , another day am saying I will never leave 

As a developer they are many reasons to love cloud computing - you know scaling, utility computing etc.

As a developer and entrepreneur in Africa, these reasons are even more profound.

Here’s why


1. Access to world class data centers.

I love it when I go to pitch my products and the smart a*** start asking me about availability – are your servers clustered? Are they load balanced? Yes they are, I say. I have some servers in Western Europe and some servers in North America. Highly available servers, I guarantee you they will be up 99% of the time, I tell them. (Should I tone down the arrogance after the Amazon debacle?)

When I did my first startup what we called our server (actually a desktop) was placed on my reading table in the corner of my room. When electricity went off (a very common occurrence in my part of the world) or the ISP had issues our service was unavailable, sometimes for weeks. We closed shop when the server was stolen.

I sleep so much better now - literally - given the noise the fan made.

2. I worry only about my applications

I am a developer ( I said that already), so server admins, dbas and types like them just get in my way (no details).

Guess what? I don’t have to worry about them anymore. No more inflated hard disk drive prices, no more pirated anti viruses that cause more harm than actual viruses. No more lost installation CDs.

No more …

All I need is visual studio and an internet connection and my applications are live and ready to roll. I don’t even know what OS my applications are running on any more. I totally love this.

3. Mobility

The only issue I have is when people ask where our offices are. Does cloud count?

I work from anywhere with an internet connection (not expensive and slow in Uganda anymore).

Home – no problem

Your office – no problem

The coffee shop – no problem

Like I said before, load shedding (FYI – For those who don’t know what load shading is) is not really an issue for me anymore. I just code from the hotel nearby that has a standby generator.

4. I seem like a genius

Cloud computing is still very much a buzz word for many IT pros I know. I don’t tell them that’s what the rest of the world calls the internet (he he). My startup runs in the cloud , it never goes down – I think I could use that line to pick up some chic’s.

I said 3 good reasons why I love cloud computing, disregard number 4.

When I can select an African country in the list of datacenters to host my application and not have my local credit card blocked from paying for service then I will get married to the cloud – come-on Azure.

 

 



Rant - Why is Windows Azure not available in Africa?

Yesterday at the .NET user group meeting in Kampala Uganda  I gave a talk on cloud computing with Windows Azure  (details will be in my next blog post).

The guys were excited. Without owning they own inftrastucture and at low cost they can build scalable , highly available applications.

Not quite.

Azure accounts are only available to people in particular countries - none from Africa.

I attended PDC in 2008 when Microsoft unleashed Windows Azure.

One of the case studies to show the benefits ofr cloud computing was a project in Africa for an education service in Ethiopia. The point they where making was that the cloud was perfect for scenarios where computing infrastructure is not sophiscated, like Ethiopia.

Perfect , i thought.

So i got my beta account from PDC and started playing around in the cloud. Then Azure goes live , my beta account does not work any more and I cant pay because am from Uganda.

Microsoft , this sucks.

I dont know the reasons for Microsoft doing this, but am sure we can work out something.

We in Africa need the cloud more than anybody else in the world. Setting up data centers that are higly scalable and available for our startups is not an option we have. But we also cant pay for cloud computing with Microsoft. Microsoft, we know we are a tiny insigficant market for a company your size, but you excluding us only continues to widen the digital divide.

Microsoft , how about you have a reseller model for cloud computing. Instead of trying to deal direclty with each client you have local partners who help you sell and bill your cloud services. I think that would lead to Windows Azure being available in Africa. I can help you resell in Uganda.



Building services with the .NET framework Cont’d

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();

        }

    }

 

 

Our WCF Service

namespace UG.Demo

{

    [ServiceContract]

    public interface IShowMeTheMoney

    {

        [OperationContract(IsOneWay = true)]

        void SendMessage(IList<string> numbers);

    }

}

 

public class ShowMeTheMoneyService:IShowMeTheMoney

    {

        TwitterService twitsvc;

        SMSService smssvc;

        public ShowMeTheMoneyService()

        {

            twitsvc = new TwitterService();

            smssvc = new SMSService();

        }

        #region IShowMeTheMoney Members

 

        public void SendMessage(IList<string> numbers)

        {

            var twit = twitsvc.SomeMethodName().FirstOrDefault();

            foreach (var item in numbers)

            {

 

                smssvc.Send(item, String.Format("@{0} {1}", twit.User, twit.Status));

            }

        }

 

 


We host our application in a Windows Console and we also support queing 

namespace UG.Demo

{

    class Program

    {

        static void Main(string[] args)

        {

            // Get MSMQ queue name from appsettings in configuration.

            string queueName = ConfigurationManager.AppSettings["queueName"];

 

            // Create the transacted MSMQ queue if necessary.

            if (!MessageQueue.Exists(queueName))

                MessageQueue.Create(queueName, true);

 

    s.

            // This is useful to generate a proxy for the client.

            string baseAddress = ConfigurationManager.AppSettings["baseAddress"];

 

            // Create a ServiceHost for the service type.

            using (ServiceHost serviceHost = new ServiceHost(typeof(ShowMeTheMoneyService), new Uri(baseAddress)))

            {

                // Open the ServiceHostBase to create listeners and start listening for messages.

                serviceHost.Open();

 

                // The service can now be accessed.

                Console.WriteLine("The service is ready.");

                Console.WriteLine("Press <ENTER> to terminate service.");

                Console.WriteLine();

                Console.ReadLine();

 

                // Close the ServiceHostBase to shutdown the service.

                serviceHost.Close();

            }

 

        }

    }

}

 

 

Thats it. We have just developed an application that can can retrieve tweets from twitter and send SMS to any number in the world.

Wrap - up

I just showed you a very simple but yet very powerful application built using open API's that i accessed from half the way the world.

I would like to encourage developers , especially Ugandan's to develop public , open , standard services (API's) for their applications.

Wilson Kutegeka who made the last presentation is the developer of clinic master  - he could make it even more powerful by opening up its functionality as open standard based API's (using WCF off course). This would allow some of the younger talented developers like David Mugume who attended the meeting to extend this software for instance by building an application where patients can get their medical history by SMS.

if Malisa Ncube who also works in the health sector built open API's then by combining the functionality of his applications with clinic master an even more powerful application might be built.

Hope you see where am going with this.

Its my opinion that "local" software engeeners can develop powerful applications by collaborating via open standard API's ; so I encourage all off you to check out WCF because it is a tool that can enable you to build these services.

Hopefully in a few months we shall start to see some localized API's.

For inspiration (or use) check out programmable web

 



Building services with the .NET framework

On the 26th of May 2010 , I made a presentation to the .NET user group meeting (thanks to Malisa Ncube for organizing this event every month … ).

If you missed my presentation , we talked about why we should all be building services … better still using the .NET framework.

This blog post is an introduction to services , why you would want to build services and how you can build services using the .NET framework.

What is a service?

OASIS defines service as "a mechanism to enable access to one or more capabilities, where the access is provided using a prescribed interface and is exercised consistent with constraints and policies as specified by the service description." [1].

If the above definition sounds to academic , you can also define a service as loosely coupled units of functionality that have no calls to each other embedded in them.

Instead of services embedding calls to each other in their service code they use defined protocols that describe how services pass and parse messages. This is a good way to think about services if you’re from an objected oriented background. While in object oriented programming functions make calls to each other, in service oriented programming, functions pass messages between each other.

Why would you want to use services?

1. If your enterprise architecture looks like this

 

Services are the building blocks for SOA .

With SOA you can move away from the sphaggetti infrastructure that is common in most enterprises.

The complexity or lack of visibility of the integration points in your enterprises makes it difficult and costly to implement new initiatives and changes into the business - and even impossible in some cases - as it is not possible to identify the impact a change in one system might have to other systems.

With services you can move to an architecture like this

Your building blocks from Spaghetti infrastructure to something that is more well-defined and manageable to achieve cost efficiency and not least business agility - enabling you to react to changes in the market with speed and achieve operational efficiency and control are services.

2. If you want to become the Gates or Zuckerburger.

Have you heard about Web 2.0 ? Mashups?Software as a service (SAAS) ? Cloud computing ?

 They all offer you the opportunity to have scalable but low cost business models and they built using services. 

Some of my favorite companies that leverage services for their business models include

Services with the .NET framework

If you are a .NET developer and you want to develop services, Windows Communication Framework (WCF) is the tool for you.

WCF is Microsoft’s unified programming model (service model) for building service oriented applications.

( Before .NET 3.0 you had several models for programming services in .NET including .NET remoting, Web services (ASMX), COM +, Microsoft Messaging queuing (MSMQ) etc, after .NET 3.0 the programming model was unified into one i.e. WCF ).

Windows Communication Framework (WCF) provides you

1. A Software Development Kit (SDK) for creating SOA applications

2. A runtime for running services on the Windows platform

Why would you use Windows Communication Foundation if you’re programming services?
 

1. WCF supports interoperable and open standards e.g. WS* protocols for programming SOAP services

2. WCF has a unified programming model.
Whether you use TCP or Http or Pipes or transmitting using Messaging Queues, programmers need to learn just one way to program.

Previously you had .NET remoting, MSMQ, Web services, COM+ and they were all done differently

3. WCF has a productive programming model
You don’t have to worry about all the plumbing involved to write services.
You have a rich declarative programming model to add stuff like logging, transactions, and reliable messages in-built in the Windows Communication Framework.

4. WCF is feature rich supporting reliable messaging, transactions , AJAX and JSON, REST and so much more.

Understanding services in WCF

The basic principles of WCF are as easy as ABC
A – Address
This is where the service is located
B- Binding
This describes how you communicate with the service
e.g. Use TCP, HTTP or both. How to exchange security information with the service etc.
C – Contract
This defines what the service can do.
E.g. Pay water bill, Make a phone call

A - Addresses

In WCF, an address is a combination of transport, server name, port and path
Example addresses may include

http://localhost:8001
net.tcp://localhost:8002/MyService
net.pipe://localhost/MyPipe
net.msmq://localhost/private/MyService
net.msmq://localhost/MyService

B- Binding
 

There are numerous ways to communicate with services , different ways that a message can be formatted/sent/secured, that allows you to tailor your service for the compatibility/performance you require for your solution.

Transport

You can use
HTTP
TCP
MSMQ ,
Named pipes,
Your own custom transport etc

Message
You  can send a
plain text
binary,
Message Transmission Optimization Mechanism (MTOM) message

Communication security

You can have
No security
Transport security
Message security etc

Behaviour

You service can support
Transactions
Be reliable
Use queues
Support ajax etc

C - Contract

You define what your service can do using

Service contracts . Operations that your service can do, communications and behaviours

Data contracts . Messages that are passed from and into your service and how they are formatted

Fault contracts . Errors types in your service

 

As an example, suppose your service shows money.

You define your service contract using a interface

[ServiceContract]
public interface IShowMeTheMoney
{
  [OperationContract]
   Money Show();
}

You define the data contract by annotating a class it with the Data Contract attribute and fields you want to pass in the message as Data Members.

(Note:- In the latest versions of WCF you dont have to use attributes if you passing all the objects properties in the message)


[DataContract]
public Money
{
  [DataMember]
  public string Currency { get; set; }
  [DataMember]
  public Decimal Amount { get; set; }
  public string Comment { get; set; }
}

Features of Windows Communication Foundation

Windows Communication Foundation is not only easy to use but feature rich , offering you several options to tweak your service to fit your business requirements.

Some of the features of WCF include

  •  Hosting options in Windows process activation server (WAS) and Internet Information Services , Windows services, Console applications , Forms and Web.
  •  Workflow services You can combine WCF with Windows WorkFlow Foundation (WWF) to write workflow type services
  • Control how your data (messages) are transferred and serialized e.g. you can serialize your business objects as XML or binary
  • Control over session management , instance creation and concurrency management without writing code if you like
  • Queues and reliable sessions. You can store messages from the sending client and later forward them to the receiving application. You can also guarantee that messages will arrive at their destincation.
  • Transactions:  You can have different services participate in a transaction operations that can be rolled back if needed
  • Security. WCF has rich features for authorization and authentication  as well as keep audit trails
     
  • Web programming model. WCF allows developers to expose services as non SOAP endpoints
  • Inbuilt features that you can use to write JSON and services that support AJAX applications

And lots more

In my next blog I will show you how to build the real world application that we demo'ed at the meeting.

 



Using strongly Typed HTML/AJAX Helpers with ASP.NET MVC

If you dont want to use strings but instead use strongly typed expression syntax in ASP.NET MVC, you can use the HtmlHelper<TModel> and AjaxHelper<TModel> types that are exposed on the ViewPage<TModel> base class.

You can then write syntax like

 <ul>
  <%foreach (Product p in ViewData.Model.Products) { %>
      <li>
          <%=Html.ActionLink<CatalogController>(x=>x.Show(p.ProductCode),p.Name) %>
      </li>
  <%} %>
  </ul>

instead of

 <ul>
  <%foreach (Product p in ViewData.Model.RelatedProducts) { %>
      <li>
        
           <%= Html.ActionLink("Product", new { Controller = "Catalog", Action="Show", Name = p.Name})%>
      </li>
  <%} %>
  </ul>

However, this feature does not come with the default ASP.NET MVC 1.0 installation , you'll have to download MVC Futures assemply from codeplex

Make sure you add the Microsoft.Web.Mvc namespace to the <namespaces> within the <pages> section of your web.config file.  This will cause the extension method to be automatically imported on all views. 
Once you do this and compile, your good to go



What is Windows Azure?

Windows Azure is Microsofts Operating System, in the cloud.

Thats a mouthfull, isnt it?

Basically, Windows Azure is a Microsoft DataCenter somewhere in the US (for now), where you can host applications you build.

You have API (HTTP && REST) access to databases, queues, blob stores that you can use to put your application together.

And if you a .NET developer, you can leverage your current skills to build applications in the cloud.

The best anology I have seen for cloud computing and the 'traditional' on premise computing is that of the  train and the truck.

Imagine you've got a farm --- with cows

To deliver milk to your customers

1. BUY A TRUCK

You have control on its schedule, how much you can load , where you can pack it etc.

However, as demand for your milk grows you've got to buy more trucks, hire more drivers, incur more packing fees etc to meet that demand. 

Furthermore, on Christmas, your demand sky rockets but because you've got limited trucks you cannot handle that spike.

In Janaury, everyones broke, demand is low, so you pack some trucks but still have to pay for packing.

Thats your traditional in - house datacenter. You have control over your data and services.

However, to scale you have to buy more hardware, probably hire more admins, worry about availabilty, worry about upgrades etc.

When you have spikes in demand, your systems grind to a halt -- as you install more servers. When demand is low, you have redudant capacity.

2. USE THE TRAIN

Its public transport, so you've got to follow there schedule, follow certain routes ----and  hey --- you've got to have access to the train station. You dont have much control over the trains, do you?

However, the train can carry lots of milk .... lots and lots of milk. The more you carry, the more you pay. You dont worry about fueling the train, servicing the train -- just get my produce there in time.  

In december when demand is high pay for more carriages. In Janaury, when demand is low, pay for less carriages.

Thats cloud computing.  Your data is not near you (its in the cloud, dah), you have limitations on what you do with your data and services.  

However, you can scale up to the level of the internet with less marginal costs than if you have your own datacenter, and you dont have worry about availability, performance etc.

As demand for your applications grows, you can pay M$ for more capacity.

 If demand shrinks, you can pay for less capacity.

Did I mention you can do this with less adminstrative overheads.


So go over to  www.microsoft.com/azure/default.mspx and sign up for a test carriage ---- sorry account.



uirrrgh --- SQL server 2008 is driving me nuts

I installed SQl server 2008 with Visual Studio 2008 recently.

After I installed Visual Studio SP1 , all hell broke loose.

Basically, I cannot connect to rem ote database using server explorer.

I get the error below

Most of the solutions talk about doing a reinstall --REINSTALL !!!!!!!!! --


Error

 



Serializing an abstract data contract

Recently I needed to serialize abstract classes in WCF.

The best way to explain it is with an example.

I have a abstract class

[KnownType(typeof(CreditCardPayment))]
public abstract class Payment
{
    private string _amount;
    private string _currency;
    [DataMember]
    public string currency
    {
        get { return _currency; }
        set { _currency = value; }
    }
    [DataMember]
    public string amount
    {
        get { return _amount; }
        set { _amount = value; }
    }
 
}

The class deriving it is

[DataContract]
public class CreditCardPayment:Payment
{
    private string _creditCardNumber;
    private DateTime _expiryDate;
    private string _securityCode;
    [DataMember]
    public string securityCode
    {
        get { return _securityCode; }
        set { _securityCode = value; }
    }
    [DataMember]
    public DateTime expiryDate
    {
        get { return _expiryDate; }
        set { _expiryDate = value; }
    }
 
    [DataMember]
    public string creditCardNumber
    {
        get { return _creditCardNumber; }
        set { _creditCardNumber = value; }
    }
 
}

When I attempted to serialize this to a message queue, I got the error below

at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write3_Payment(String n, String ns, Payment o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write5_AirTimeOrder(String n, String ns, AirTimeOrder o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write6_AirTimeOrder(Object o)


The solution is to the code below

[DataContract(Namespace = "www.pro.com")]
[KnownType(typeof(CreditCardPayment))]

[System.Xml.Serialization.XmlInclude(typeof(CreditCardPayment))]
public abstract class Payment
{
    private string _amount;
    private string _currency;
    [DataMember]
    public string currency
    {
        get { return _currency; }
        set { _currency = value; }
    }
-------------------------------------------------------------------------------