oAuth for IMAP and Gmail in C#

I looked for this one around the net for a long time and ended up implementing it myself so thought I should share it:

If you are going to use oAuth authentication with Gmail using IMAP in C# the you’d need to sort out two things:

  1. Get Access Token and Token Secret from Google
  2. Use an AUTHENTICATE method in IMAP protocol

This code uses dotNetOAuth for the oAuth part to get the access token and secret code. As for IMAP implementation I use LumiSoft IMAP and added AUTHENTICATE support to it.

Assuming you have the Consumer and TokenManager that you’ve used to get the AccessToken and TokenSecret, this will help you to build the XOAUTH parameter needed:
You then need to implement this method in LumiSoft code to allow IMAP AUTHENTICATE:

This will go after the Login method of IMAP_Client.cs

We are running out of domain names

This is not something new. Anyone who has ever wanted to register a domain name knows that it is impossible to get the name you want and next to impossible to get anything meaningful. Hence the web startups with strange names like Spotify, misspelt like Flickr, outright weird gnolia (of ma.gnolia.com) and also the trends of adding suffixes to domain names like “fy”, “sy” and so on.

This is almost entirely fault of domain squatters who pay peanuts for domain names and sit on them forever until a buyer is desperate enough to shell out thousands of dollars for the hard work. To ease this pressure ICANN adds new top level domain names or remote islands sell the rights to their top level names to companies (.tv for example)

I personally think the only way out of this is to increase the price of .com registration – a lot. A $200 .com domain (like it used to long long ago) makes squatting very expensive and also stops people from panic buying domain names in case they might need them in the future.

Come on ICANN, go against the market pressures for once!

Amazon CloudFront CDN and ASP MVC

I needed to use a CDN in my ASP MVC app and needed it to be easy to develop against. I chose Amazon CloudFront since our servers are running on Amazon EC2 and we are also using Amazon S3 during our deployment. You can get a good introduction and step-by-step on CloudFront here.

As for the ASP MVC side, I wanted to have something like Url.Content where it converts my URL to my CDN URL while allowing for multiple development environments.

Here is how I did it:

A helper extension class on System.Web.Mvc.UrlHelper to add a new method to Url: This one is CdnContent

Here is an implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web.Mvc;
using System.Configuration;

namespace CdnHelpers
{
public static class CdnHelpers
{
private static AppEnvironment _appEnvironment = AppEnvironment.Unknown;
private static string _cdnBase = string.Empty;

private static AppEnvironment AppEnvironment
{
get
{
if (_appEnvironment == AppEnvironment.Unknown)
{
_appEnvironment =
(AppEnvironment)
Enum.Parse(typeof (AppEnvironment), ConfigurationManager.AppSettings["App.Environment"], true);
}

return _appEnvironment;
}
}

private static string CdnBase
{
get
{
if (string.IsNullOrEmpty(_cdnBase))
{
_cdnBase = ConfigurationManager.AppSettings["Cdn.Base"];
}

return _cdnBase;
}
}

public static string CdsContent(this System.Web.Mvc.UrlHelper url, string contentPath)
{
if (AppEnvironment == AppEnvironment.Local)
{
return url.Content(contentPath);
}
else
{
contentPath = contentPath.TrimStart('~');

return string.Concat(CdnBase, "/", AppEnvironment.ToString().ToLower(), contentPath);
}
}
}
}

AppEnvironment is an Enum:

    public enum AppEnvironment
    {
        Unknown,

        Local,

        Test,

        Dev,

        Staging,

        Production
    }

You need to add two keys to AppSettings of the Web.config:
App.Environment to determine which environment the code is running as and
Cdn.Base which would be something like http://cache.mydomain.com (no trailing slash)

Also add the namespace of the helper class to System.Web/Pages/Namespaces under Web.config

This should be it. Now you can replace a Url.Content with Url.CdnContent in the code.

I didn’t want to redirect all files by their extension to CDN but that’s not difficult to implement.

On deployment to Amazon:

  • Amazon CDN files and folders are case sensitive.
  • Create folders with the name of your environments under the root of the S3 bucket and deploy the files and folders there.

Open Gardens, Closed Clouds

At my company, we are building a fantastic personal search engine (still in private beta. Stay tuned!). As part of our architecture, we needed to cater for scalability and redundancy from the very beginning, so we went for cloud hosted services. We wanted to be able to deploy any of the layers of the system within 15 minutes to cope with the load. Our system is running on Windows OS and is written in .NET 3.5. We tried GoGrid, some smaller Hyper-V VPS providers, Windows Azure and Amazon EC2.

I wrote some time ago about what I think about Azure at this point in time. Since then we tried standard Windows OS cloud providers and haven’t had time to play with Azure again. I’m sure it is going to improve. However, the acquisition of Atebits (the guys behind Tweetie) by Twitter made me think about Microsoft and Amazon again.

Building companies on Microsoft technologies is a risk. You are exposed to Microsoft going bust, changing its direction, etc. But how likely is that? As far as a small business is concerned, negligible. You can safely build a small/medium business and be exposed to Microsoft. Amazon is smaller than Microsoft but for me it is big enough. So I don’t worry about exposing myself to Amazon going bust. But there is one difference between building my business around Azure and building it around Amazon EC2: Lock-in.

Azure locks me in. I need to make changes to my product for it to work in Azure. Amazon EC2 on the other hand is just commoditized Windows Server horsepower. If Amazon decides to raise its prices to a level that is not affordable to me, I can switch to another dozen providers giving me same stuff. And that’s a big problem as your business gets bigger.

The other day some people from a big data integration company were giving us a presentation about their middle-tier systems. The said their system can run on Amazon EC2 and the CTO of the company wasn’t sure if he wants to build exposure against Amazon. I see it another way though: Being exposed to Amazon in this way is the same as being exposed to your ISP. They can go down and take you out of business for a week. But you CAN recover within a reasoable amount of time. Building my business around Azure isn’t the same.

Risk = Probablity x Impact

The probablity of Microsoft making breaking changes to Azure is very low but the impact is very high. The probablity of GoGrid going bust is much higher, but the impact is very low (I can switch or have a backup on another cloud provider).

As it stands, running your business on Azure is like building a business around Twitter. This can change if Azure changes to a more commoditised computation power provider or allows other companies to provide Azure as a service.

On letters, stupid websites and Windows 7 OCR

I like many people get shed loads of letters from anyone who doesn’t care about the trees. Yesterday I got a letter from O2 (my mobile network) saying that they are going to stop sending me paper bills and instead I have use their website or iPhone app to check my bill.

Typical for big corporates stuck in old days, not only did they create a user name for me, they used my abbreviated name with a random four digit number as they user name. I now have to use this username and come up with a password that has at least 5 different “security” requirements. A website that I am going to use once or twice a year with a very bad user name and a worse password means disaster.

This time instead of putting the letter in my draw, I decided to use my Brother DC-540CN printer/scanner and scan the letters. This scanner has an automatic document feeder which makes it even easier. But the beauty of it was when I realised I can scan the letters to a TIFF file and turn on Windows 7 OCR feature. Seconds later, I could type O2, mobile or iPhone in my Start Menu and a scan of the letter from my stupid network provider popped up instantly. Brilliant!

WindowsOCR

Hoptoad client for .NET and log4net

Hoptoad is a web based application that captures your application’s exceptions and errors. It has some simple de-duplication algorithm and forwards new exceptions to your email box. It also tracks your deployment and resolved exceptions.

It is build for Ruby projects but it can be used for .NET apps as well (I wish people behind it would make it more generic than what it is now). For ASP .NET applications, there is another similar application called Exceptioneer but it doesn’t have an open API (you need to add their assembly to your project) and is very isolated to ASP .NET web applications. It is also in private beta.

I use log4net for almost all my projects, so I thought I should write a Hoptoad appender for log4net. The Hoptoad client itself is not locked into log4net and can be used standalone as well. Download the code here

Perhaps the next step is to write the same thing for ELMAH.

To configure the log4net appender use something like the following section:

        <appender name=&amp;quot;HoptoadAppender&amp;quot; type=&amp;quot;SharpHop.HoptoadAppender, SharpHop&amp;quot;>
            <ApiKey value=&amp;quot;xxxx&amp;quot;/>
            <EnvironmentName value=&amp;quot;Dev&amp;quot;/>
            <AppName value=&amp;quot;Your app name&amp;quot;/>
            <AppVersion value=&amp;quot;1.0.0.0&amp;quot;/>
            <AppUrl value=&amp;quot;http://yoururl.com&amp;quot;/>
            <layout type=&amp;quot;log4net.Layout.PatternLayout&amp;quot;>
                <conversionPattern value=&amp;quot;[%thread] %-5level %logger{1}: %message&amp;quot; />
            </layout>
        </appender>

UPDATE: SharpHop now has a home at GoogleCode as an open source project with Apache Licene.

The Microsoft way

Facebook has a HTTP REST API exposed to the outside world. It is used by thousands of developers in different languages. Google API for most (if not all) of its services is also based on HTTP REST. This seems to be working for almost any “web 2.0″ application out there. Now bring Microsoft into the complex: Facebook SDK by Microsoft. I’m trying to get the news feed from a multi-threaded application using this SDK and here is what I get: System.Threading.ThreadStateException: ActiveX control ‘8856f961-340a-11d0-a96b-00c04fd705a2′ cannot be instantiated because the current thread is not in a single-threaded apartment.

And for now I’m getting this in my unit tests (which are running under ReSharper), I am going to have so much “fun” doing it in the real app.

I just want to ask WHY? Why is there an ActiveX anywhere in this SDK? Forget about the old and redundant technology. Couldn’t MS guys put a simple SDK together like ANY other REST based SDK out there that just works based on a bundle of wrappers around HTTP REST calls?

Why does Microsoft want to do everything their way and ignore every single standard under the sun?

Chrome Apps issue in Windows 7 Taskbar

I love Windows 7. So much that when my MacBook batter died for the third time in two years because of a manufacturing fault, I didn’t bother to go to an Apple shop again! I now use my Samsung NC10 netbook with Jolicould pre-beta and Windows 7 on my main desktop.

I also love Chrome as a browser. I’m using the main release and not Dev or Beta channels since it’s my main browser and I have other things to worry about at the moment. However, one thing bugs me a lot about Windows 7 and Chrome. Here is a snapshot of my taskbar. I have a Chrome App for my Last.fm and ever time I start it, all other instances of Chrome are displayed under that as multiple tabs.

Windows 7 Taskbar and Chrome apps

Chrome Team: Please fix this! Thank you!

Microsoft Azure is not ready for prime time yet

My recent project was hosted on a virtual server in GoGrid. The server was a Windows 2008 running IIS7 and SQL 2008 Express. The application was in .NET 3.5 SP1 and was running as a ASP MVC front end and a Windows Service as a back end. NHibernate as ORM, Spring.net for IoC and DI, Log4Net for logging and a few other open source tools and frameworks.

With my BizSpark partnership approved, I tried to move the system to Windows Azure and now after spending 2 weeks on that I am moving it back to a dedicated server and trying to recover from the time wasted on this.

In my opinion, Azure is a brilliant framework but it is not ready for prime time yet. It can become a great cloud development environment within a year or so, but not now.

The problem is Microsoft is inventing lots of new concepts and tools to make Azure happen. The logging framework, communication between the worker instances (done only through WCF and queues) and many other new concepts are being tried at the moment as Azure is in CTP. However, this means no serious commercial product can really be ported to it until the tools and framework is set and gets to a certain level of maturity. Until then, developers will lose a lot of time, tracking this moving target and that is something many startups cannot afford.

Compare this with other MS technology based cloud computing services offered by Amazon, GoGrid, RackSpace and others. These are based on tried and tested technologies and that makes it easier for developers to develop for.

Cloud computing hosting and development is still a new technology and is still suffering from teething problems. I think Microsoft is doing a good job in inventing new tools for this new paradigm. However, it should try to open up its standards and to the open source standards if it wants to see Azure take off and more importantly, it should stay in the game for a long time and the developers will gradually move to the new platform slowly.

For now, the pain of moving my application to Azure was too great: Front-end/Back-end comms had to be replaced with MSMQ backed WCF, Lucene directories had to be re-written for Azure storage, logging had to be replaced with traces and some other issues.

Very long deployment process, VS SDK issues, SQL server accessibility problems and lack of good personal development/business support structure within Microsoft added on top of my other problems meant that I will be visiting Azure sometime later this year but not for this project.

Windows Notification Platform

Comments on the previous posts (here and here) about Growl for Windows and Snarl has made me realise that the problem is not about the frameworks, it is about platforms. Let’s get one thing straight: Windows doesn’t have a notification framework, it only has components of it, mainly a place for Tray Icons and that is hardly enough.

A framework for notification should allow developers to delegate their notification tasks to it and forget about them. Users can choose the minimum level they want to get notified about, how they would like these to be displayed/sent, when are the “quite” hours and so on. These are problems that keep popping up in most UI notification scenarios and without a framework in place, developers have to address them separately every time themselves. This means these are addressed inconsistently since each programmer sees the problem in light of different use cases.

Growl started its life for Mac as a solution for the lack of a “system tray”. It was widely adopted by developers since the alternative was to write the system tray themselves. This subtle point makes all the difference between the original Growl and Growl for Windows. Growl for Windows tries to be a clone of Growl and thinks the problem of market penetration is down to the fact the the original Grown has enjoyed a longer life. Give it enough time and it will become popular they claim.

I don’t think that way. The relative low cost of using System Tray in Windows means many developers are not going to bother using GfW as their notification tool. Add this to the relative cost of learning how to integrate GfW/Snarl into your app and it becomes less attractive.

The reason Growl for Mac was successful was that developers had incentive to use it. GfW/Snarl as they stand, offer very little incentive and I believe the current adoption rate of these tools is a result of that. This means GfW and Snarl should rethink their offerings. They should change them from mere tools to “platforms”. This means a better and tighter end-to-end experience for users and developers.

Currently not many Windows apps use GfW or Snarl natively. This means there are going to be loads of “bridges”. These bridges define the user experience for both tools and at the moment neither of the tools offer much to control these bridges.

GfW and Snarl should offer centralized configuration for plug-ins. Make installation and upgrade of plug-ins dead easy and integrated. Have a central “app store” like plug-in directory and control who is putting the plug ins on it. If a plug in installs its DLLs directly into Snarl extensions folder and therefore breaks the uninstall process of the tool as well as causing other issues for the user, it should be stopped. Now it is not a question of Apple App Store like policing. It can be done easier by forcing the use of a controlled set of APIs that offers the above features as well as blocking “DLL droppers” to work. If a dropped DLL cannot work, no developer will bother doing it and they will go through the proper way which is actually easier for them as well since they don’t need to worry about installation and in most cases even any UI exposure.

A range of high quality bridges will help the adoption rate which in turn give developers more incentives to leverage the large user base GfW and Snarl will enjoy. The rest will be a self fulfilling prophecy!