Deploying your ASP.NET MVC3 site on Mono

In this blog post I am going to show you how easy it is to create a new ASP.NET MVC site and deploy it on Linux/Mono.

Step 1 – Create project
Create a new ASP.NET MVC 3 project. Compile and run it.

Read more Deploying your ASP.NET MVC3 site on Mono

One week with the Nokia Lumia 800

One week ago I got a Nokia Lumia 800 from my company, this is my random thoughts so far:

+ Amazing screen
+ Good batery life for being a smart phone
+ Really beautiful
+ User interface is blazing fast
+ Integration with Google, Facebook and MSN works great. All my contacts was synced from Google.
+ Fast web browser, zooming and scrolling is super smooth
+ Live tiles are really cool, but not sure if it is the best use of space
+ Hubs! This is a great concept, messages goes to the messages hub, music to the music hub and so on.

– The lack of applications, good thing i am not a big app user
– Applications are not as mature as on iOS or Android. For example the Facebook application is missing a lot of functionality found on iOS and Android.
– The default search engine is Bing and I can’t find a way to change it.
– Navigating back in the web browser. Navigating back to the previous page is done by clicking the back button in the bottom left corner, works great if you are surfing some pages, but if you leave the browser and come back, the back button will take you back to the main screen (or where you were before going back to the web browser). At this point there is no way to navigate back in your surfing history, really dumb!

But over all I must say I am very happy with it!

Node.js: Callbacks are polluting your code

I have been hacking on a project in Node.js/Express.js for some time now. I am really impressed by how fast it is to code and prototype in Node, not much gets in your way. However, there is one thing I am still struggling with, the callback model of Node.js applications. It is not that it is conceptually hard to understand or use, but i feel that it keeps me from writing clean code.

Let’s imagine that we are writing a small nonsense program. The program receives a post, create some variable x, if some condition is true we call an async method to get some result and assign it to x.result. In the end we want to save x (also async). This would probably be my first attempt in node:
 

app.post('/someurl', function(req, res) {
var x = {..}

if (some contidion) {
  someOperation(function(err, result)) { 
    x.result = result
    x.save(function(err, result) {
       if (err) next(err)
       res.redirect(url)
    })
  })
} else {
  x.save(function(err, result) {
    if (err) next(err)
    res.redirect('/')
  }
}

 

(Edit: To clarify, both someOperation and save is doing some kind of I/O)

So is this clean code? In my opinion it is not. Read more Node.js: Callbacks are polluting your code

MongoDB, C# and Mono

MongoDB is one of the many free open source NoSQL databases that exist today. I wanted to try out how well the official drivers for C# worked when using Mono.

On the MongoDB web site they have pre built binaries of MongoDB for almost every platform, i grabbed the 64 bit binary for OSX. No installation is required, just unzip the file and run the “mongod” binary from the directory. There is also a binary called “mongo”, this is the mongo interactive shell which is very useful.

The C# drivers are available, both in binary form and as source, from the C# language center. To get started I fired up MonoDevelop 2.8 running on Mono 2.10.8:



MonoDevelop has matured a lot since I used it the first time. It is still not as good as Visual Studio, but it is getting there. What takes most time to get used to is the totally different set of shortcuts, and the lack of all ReSharper magic.

Let me break down a simple console application example on how to use MongoDB using the official driver from 10gen: Read more MongoDB, C# and Mono

Platform as a Service vs Infrastructure as a Service

This week I attended a tech talk about cloud computing and more in general about Microsoft Azure. It got me thinking a bit regarding PaaS vs IaaS (= virtual servers). Let me give you my thoughts.

PaaS – The good

No servers

You do not have to deal with servers, virtual or physical. You work with resources.

No software upgrades

No OS of software upgrades.

Easy scaling

Scaling your infrastructure is already taken care of for you. You just need to make sure you follow the guide lines for developing scaleable applications on that platform.

PaaS – The bad

Limits your software stack

You might not be able to pick the best suited application server/web server/caching solution/database/other great software for your application. You are often limited in some way to what is provided by the PaaS.

Portability and lock in

Several of the PaaS solutions today require you to develop for that platform, for example Microsoft Azure and Google AppEngine. If you want to move your application anywhere else, you are out of luck. There is only one provider – so then you will have to rewrite your application (or at least parts of it), to be able to run it somewhere else. If you are using services provided by the PaaS that is not available somewhere else, for example Azure SQL Server or Googles Bigtable database, it might get even harder.

Pricing can be confusing

Can be hard to estimate how much running your application will cost. Microsoft Azure is a good example of a confusing price model.

IaaS – The good

Flexibility

Full control over your software stack, if you want to switch to a more cutting edge web server or cashing solution, go ahead.

Portability

If you need, it is easy to move to another provider of IaaS, to a private cloud or even to physical servers.

Easier to estimate your cost

Most PaaS you pay per computing hour of each virtual server, per GB of stored and transfered data.

IaaS – The bad

Administration

You will need sysadmins to manage your servers, just like normal physical servers

Scaling

You will have to build a scaling infrastructure on your own.

 

Conclusion

The PaaS idea is great, but it means giving up too much control and flexibility over your application, and what is worst – it locks you into a provider. I know that Microsoft is working towards enabling private Azure hosting, this would allow you to host your Azure application in your private company Azure cloud, or with Microsoft, or other providers. I hope other providers move in this direction as well. Private PaaS hosting would allow us to develop our application and run it anywhere, but with IaaS we can already do this today!

To conclude things, I might be old school, but I still prefer IaaS to PaaS.

String interning in C#

Last week a coworker sent me an interesting piece of code:

const string a = "";
const string b = "";
const string c = "hello";
const string d = "hello";

Console.WriteLine(ReferenceEquals(string.Empty, string.Empty) 
  ? "Not so surprising…" 
  : "Oh, the humanity!");

Console.WriteLine(ReferenceEquals(a, string.Empty) 
  ? "But I thought string was a reference type!" 
  : "Seems fair…");

Console.WriteLine(ReferenceEquals(a, b) 
  ? " But I thought string was a reference type!" 
  : " Seems fair…");

Console.WriteLine(ReferenceEquals(c, d) 
  ? " But I thought string was a reference type!" 
  : " Seems fair…");

Running the program will, maybe a bit surprising, produce:

Not so surprising…
But I thought string was a reference type!
But I thought string was a reference type!
But I thought string was a reference type!

The reason to this is what is called String interning. The CLR basically holds a hashmap of all the strings in the program (edit: string literals), one entry per unique string. So two identical strings will always have the same reference even though they are defined in different places. String interning is mainly used to speed up string comparisons (no need to check char by char if they are identical, if they have the same reference they are equal), but also to reduce the memory footprint of the application.

Read more about string interning:
http://aspadvice.com/blogs/from_net_geeks_desk/archive/2008/12/25/String-Interning-in-C_2300_.aspx
http://en.wikipedia.org/wiki/String_interning

SignalRChat – Using RxJS to add live notificaitons

I have been playing around a little with Reactive Extensions for javascript and must say that I am really impressed. RxJS a port of the .NET based version which is, as they describe it on their site, “a library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.“. So what does this mean? Let me give you an example.

I wanted to add a live notification function to the chat, as soon as someone is typing, three dots should appear next to the user’s name. This means that as soon as the user starts we need to send a message to the server, this message is then broadcasted to all the connected clients. To not flood the server and the users, maximum one message per second should be sent.

Keydown – time since last message >= 1 second, or this is the first keydown in the sequence => send message
Keydown – time since last message < 1 second => do nothing

There is a built in method called Throttle that does almost this, we could use it, but then the first keystroke would get delayed and I did not want this. So being a beginner at RX I struggled for a while trying to get to where I wanted. I came up with a solution that did what it I wanted, but it was not so elegant. Then I found this post on StackOverflow, with a solution by Sergey Aldoukhov that solved the problem in a very elegant way, so I just decided to use it instead. Sergey had created an extension method called OneInTime, this is the code:

Rx.Observable.prototype.OneInTime = function (delay) {
    return this
        .Take(1)
        .Merge(Rx.Observable.Empty().Delay(delay))
        .Repeat();
};

So what does this mean? Read more SignalRChat – Using RxJS to add live notificaitons

Updated version of SignalRChat

Yesterday I uploaded a slightly updated version of the test application I have built using SignalR. Besides from looking much better, it now uses the high level Hubs API (instead of the connection API) that SignalR provides.

You find it here: http://signalrchat.apphb.com/

And you can find the source on GitHub: http://github.com/moberg/SignalRChat

SignalR – Real time web for .NET

SignalR is a library for .NET to help build real time web applications, in a way it is quite similar to Socket.IO on Node.js. It consists of three main parts:

  • SignalR.Server – .NET code running on IIS
  • SignalR.Js – A SignalR client in javascript
  • SignalR.Client – A SignalR client in .NET. Useful for communicating with a SignalR server from, for example, a Windows Phone application.

SignalR is using long polling as a method to keep its http connections open. In the future support for WebSockets is going to be added as well (a guess would be when .NET 4.5/IIS8 is released, it will have support for WebSockets).

Let me show you how simple it is to create a chat server. First start Visual Studio and create a ASP.NET MVC3 application. When you have created your project you want to add SignalR from NuGet, so open up the NuGet manager and search for SignalR. You need the SignalR.Server and SignalR.Js packages.

Read more SignalR – Real time web for .NET

My first Google Chrome Extension – E24.se cleanup

Occasionally i read the swedish news site e24.se. One thing that really annoys me about the site is the fixed banner they have on top of all the article pages. I decided to do something about it so i created a Google Chrome Extension that removes the banner, and as a bonus it also removes all the ads on the page. Enjoy:

https://chrome.google.com/webstore/detail/lffoakndkbfaoglgbnaagjglhhlapdnk?hl=sv&hc=search&hcp=main

Older posts
Newer posts