About the author
Boaz I'm a software developer, working at a small company in the Netherlands. Currently I'm mostly using techniques like .NET, C#, SQL and jQuery, but I have experience with JAVA and PHP as well.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Last week I learned something new and very powerful about the dynamic keyword in C#. I already was aware of the possibility of creating your own dynamic object by deriving from the DynamicObject class. Here is a short example.
class MyDynamicObject : DynamicObject
 Dictionary<string, object> _Values = new Dictionary<string, object>();

 public override bool TryGetMember(GetMemberBinder binder, out object result)
  if (_Values.ContainsKey(binder.Name))
   result = _Values[binder.Name];
   result = null;

  return true;
 public override bool TrySetMember(SetMemberBinder binder, object value)
  if (!_Values.ContainsKey(binder.Name))
   _Values.Add(binder.Name, value);
   _Values[binder.Name] = value;

   return true;

Now you can create instances of MyDynamicObject and just set any member variable you want and access those as well, however you should be careful or runtime exceptions will occur.

dynamic test = new MyDynamicObject();
test.FirstName = "Boaz";


// RuntimeException:
You don't even need to create your own MyDynamicObject, but you can use System.Dynamic.ExpandoObject. It even implements IDictionary<string, object> so you can foreach over its members. More information about the ExpandoObject.

The above example with ExpandoObject:
dynamic exp = new ExpandoObject();
exp.FirstName = "Boaz";

// RuntimeException:

This works because the compiler converts the dynamic type to classes in Microsoft.CSharp.RuntimeBinder and System.Runtime.CompilerServices. These classes will invoke the DLR at runtime.

But what I didn't know is that you can't only do this for classes that inherit from DynamicObject, but for any object. A very simple example:

class Program
 static void Main(string[] args)
  Console.WriteLine(Add(1, 2));
  Console.WriteLine(Add("Bo", "az"));

 static dynamic Add(dynamic a, dynamic b)
  return a + b;

This is also known as Duck Typing, what means you identify an object by its members rather then by its Type or Interfaces it implements. Just like you most likely will identify a bird that walks, swims and quacks like a duck as a duck.

When you use the dynamic type the compiler will not check your properties, methods or even operators, but will leave it for the DLR to process it. This can cause very nasty runtime exceptions, so be careful just like you would when you would typecast an object.

View the full example on gist.

April 17, 2013 - Comments [3] - Posted in .NET | C# | DLR | dynamic
My Raspberry pi is used almost daily with Raspbmc, and I mostly control it with my android phone. I had some Wiimotes laying around and an old bluetooth device so i thought it would be nice to be able to use the those as an alternative.

First I started with a program called wminput, but I couldn't get it to work in XBMC, only in the console (what is pretty much useless). But then I read somewhere that I sould have used WiiUse_WiiRemote of which the source is in the XMBC repository. So I decided to try to build it and guess what? It works!

Here are the steps I executed to make the Wiimote work with Raspbmc:

First of all we need a USB bluetooth device and get it to work. Once you plugged in the device the output of the lsusb command should contain a bluetooth device.

Now we have to make the device work, run:
sudo apt-get update
sudo apt-get install bluez
If this gives you an error you can fix this by running:
sudo update-rc.d -f dbus defaults
Now we have to get the XBMC source to compile WiiRemote, run:
sudo apt-get install libbluetooth-dev g++ libcwiid1 xbmc-eventclients-common make git-core
mkdir /usr/local/src/xbmc
git clone git://github.com/xbmc/xbmc.git /usr/local/src/xbmc
cd /usr/local/src/xbmc/tools/EventClients/Clients/WiiRemote
Now we have to do a small modification to the Makefile:
modify MakeFile to add "-l bluetooth" on the line "$(OBJS) -o $(BIN)"
$(OBJS) -o $(BIN) -l bluetooth
Now we can build it by running:
sudo make
We are ready to test if our setup works, to do this we stop XBMC and restart it when the Wiimote is connected run:
sudo initctl stop xbmc
/etc/init.d/bluetooth restart
cd /usr/local/src/xbmc/tools/EventClients/Clients/WiiRemote
Now press button 1 and 2 on the Wiimote to connect and when only the first led is on run:
sudo initctl start xbmc
Congratulations, you should now be able to control Raspbmc with your Wiimote! The only thing left to do is make it start when you power on your Raspberry pi and optionally, only allow certain Wiimotes to operate Raspbmc.

Therefore we have to find the bluetooth address of the Wiimote, we can do this by running:
hcitool scan
We create a file that will be run at startup:
nano /home/pi/wiimote.sh
The contents will be:
/etc/init.d/bluetooth restart
sleep 2
cd /usr/local/src/xbmc/tools/EventClients/Clients/WiiRemote
./WiiUse_WiiRemote --btaddr XX:XX:XX:XX:XX:XX
Where XX:XX:XX:XX:XX:XX is your Wiimotes bluetooth address.

Give the file execute permissions:
chmod +x /home/pi/wiimote.sh
And edit rc.local to run it at start up:
sudo nano /etc/rc.local
Just before the last line that reads "exit 0" add:
sh /home/pi/wiimote.sh &
January 23, 2013 - Comments [14] - Posted in Raspberry pi | Raspbmc
Almost a year ago Scott Hanselman blogged about a NuGet package called AspNetSprites that automatically generates sprite images for you. When using a lot of small icon images, sprites can speedup your website quite a lot. This is due to minimizing the number of HTTP requests.

For AspNetSprites there are helper packages for both WebForms and ASP.NET MVC, but wouldn't it be nice to use this with MonoRail too? This is not to difficult to accomplish because there is a AspNetSprites-Core package containing all the logic so all we need to do is write a MonoRail View Helper class.

I downloaded the source from the ASP.NET codeplex page and modified the Razor view helper so it can be used with MonoRail. Here the result:

using System;
using System.Linq;
using System.Text;
using Microsoft.Web.Samples;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Collections;

namespace MonoRail.Samples.SpriteHelper
    public class Sprite
        private static Control helperControl = CreateHelperControl();

        public static string ImportStylesheet(string virtualPath)

            if (Path.HasExtension(virtualPath))
                virtualPath = Path.GetDirectoryName(virtualPath);

            HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current);

            string cssFileName = ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) ?? ImageOptimizations.LowCompatibilityCssFileName;

            virtualPath = Path.Combine(virtualPath, cssFileName);
            string physicalPath = HttpContext.Current.Server.MapPath(virtualPath);

            if (File.Exists(physicalPath))
                StringWriter sw = new StringWriter();

                using (HtmlTextWriter html = new HtmlTextWriter(sw))
                    html.AddAttribute(HtmlTextWriterAttribute.Href, ResolveUrl(virtualPath));
                    html.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
                    html.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
                    html.AddAttribute("media", "all");

                return sw.ToString();

            return String.Empty;

        public static string MakeCssClassName(string pathToImage)
            return ImageOptimizations.MakeCssClassName(pathToImage);

        public static string Image(string virtualPath)
            return Image(virtualPath, null);

        public static string Image(string virtualPath, IDictionary htmlAttributes)

            HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current);

            StringWriter sw = new StringWriter();

            using (HtmlTextWriter html = new HtmlTextWriter(sw))
                if (htmlAttributes != null)
                    foreach (DictionaryEntry entry in htmlAttributes)
                        html.AddAttribute((entry.Key ?? String.Empty).ToString(), (entry.Value ?? String.Empty).ToString());

                if (ImageOptimizations.LinkCompatibleCssFile(httpContext.Request.Browser) == null)
                    html.AddAttribute(HtmlTextWriterAttribute.Src, ResolveUrl(virtualPath));
                    html.AddAttribute(HtmlTextWriterAttribute.Class, ImageOptimizations.MakeCssClassName(virtualPath));
                    html.AddAttribute(HtmlTextWriterAttribute.Src, ResolveUrl(ImageOptimizations.GetBlankImageSource(httpContext.Request.Browser)));


            return sw.ToString();

        private static Control CreateHelperControl()
            var control = new Control();
            control.AppRelativeTemplateSourceDirectory = "~/";
            return control;

        private static string ResolveUrl(string path)
            return helperControl.ResolveClientUrl(path);

This helper exposes three methods (and one overload) here are some usage examples:

<li class="$Sprite.MakeCssClassName("~/App_Sprites/categories/dotNet.png")"><a href="#" class="categories">Programming</a></li>

<a href="#">$Sprite.Image("~/App_Sprites/popular/visualStudio.png", "%{alt='visualStudio'}")
Besides this code file a small change to the web.config file is required and you should off course add the [Helper(typeof(Sprite))] attribute to your controller.

To get you started here is the NuGet packages I created for personal usage:

January 1, 2012 - Comments [0] - Posted in ASP.NET | MonoRail
FireDotNet is now available as an official NuGet package on nuget.org:

FireDotNet is a library I created to show ASP.NET server-side log messages in the Firebug console on the client-side. A few months ago I updated the library to a NLog target so all NLog messages are send to Firebug.

I used this opportunity to make some changes too. First of all I automated the builds using TeamCity. I installed a NuGet plugin for TeamCity so dependencies are automatically resolved. This means that I no longer need to have the NLog.dll files under version control. Besides managing dependencies it can also automatically make a NuGet package and publish it to both my private NuGet server and the official NuGet server.

Another nice feature of TeamCity is that it can manage version numbers for you. There is a build feature named AssemblyInfo patcher and - as the name already implies - it modifies the version number of your AssemblyInfo.cs files. So from now on all FireDotNet builds will have a version number that consists of:
MajorVersion.MinorVersion.BuildNumber.SvnRevisionNumber eg.
Where I control the first two numbers and the last two are automatically set by TeamCity.

Last but not least I managed to add a new feature too! From now on FireDotNot by default only outputs to requests form localhost. This means that you, when deploying your project, no longer have to think of removing FireDotNet from it. Except of course if you changed the allowRemote property.

December 11, 2011 - Comments [0] - Posted in ASP.NET | FireDotNet | NLog | NuGet
I read somewhere that it's very easy to backup a site using wget on linux. That made me realize that, at the moment, I do not have any backups of my sites. The only backups that (hopefully) exist are the ones my shared host creates. So I figured it would be safe to have my own backups. Since I run my own Ubuntu home-server, already for a few years now, I have a backup target too.

The only problem left is: the databases. Luckily dasBlog does not use a database to store it's posts, but a XML datastore. This makes it less painful to make a backup, especially on shared hosting. My MySQL databases can be accessed externally, so they can be backuped using a (separate) script too. Currently I already do this for my e-mail database.

So, I created the following script:

cd "$(dirname "$0")"

backupfile=$(date +%Y%m%d)
savetodir=./backup-$(date +%Y)/

if [ -d $host ]; then
    rm -r $host

wget --recursive --level=inf --quiet --user=$user --password=$pass ftp://$host/

if [ $? -ne 0 ]; then
    echo "wget faild during the backup of $host" >&2
    exit 1

tar -cf $backupfile.tar $host && gzip -c $backupfile.tar > $backupfile.tar.gz && rm $backupfile.tar

if [ $? -ne 0 ]; then
    echo "faild to create a tar.gz during the backup of $host" >&2
    exit 1

if [ ! -d $savetodir ]; then
    mkdir $savetodir

mv $backupfile.tar.gz $savetodir

exit 0
Fist a few variables are set, like the ftp site, username and password. I created a special backup user that only has read privileges, since no writing is required when making backups.

By default wget creates a directory named after the host, so I start with removing that directory and it's contents but only if it exists (from a previous backup).

This is follwed by the wget command that does a fully-recursive download (note the: --level=inf) after finishing the exit code of wget is checked for errors. The backup is then compressed and stored in a directory per-year.

Finally I scheduled a cronjob, to automatically create a backup once a week:
0 3 * * 3 boaz sh /mnt/array1/backup/n3rd.nl-sohosted/backup.sh

November 21, 2011 - Comments [0] - Posted in Backup | Ubuntu

The first question that comes to mind when creating a blog is off course why? But that is not the question I'm gong to answer, not now any way, maybe I'll get back to that later. This blog should be mostly technical and that is where this series is about.

First of all, to start blogging I needed to choose a blogging application. At first I started with Blogger but soon I realized, I am a technician, I have my own domain and I'm not even hosting my own blog?

So the next step was to search for the proper application, should it be WordPress like any other blog uses? No, I prefer an ASP.NET solution. And then I stumbled upon DasBlog. DasBlog seemed good to me, and hey Scott Hanselman is using it too!

I decided to install DasBlog and see for my self if it will work. And guess what? It does! But not without some trouble at first.

I uploaded the files, made the required security changes and visited the url. But it didn't work and gave me a very helpful *cough* "System.Security.SecurityException". I'm hosting my site at SoHosted for years now and I'm a very satisfied customer. I knew SoHosted only allows applications running in medium-trust, and I read before I started that DasBlog does run under medium-trust, so what is the problem? After searching for a while and almost giving up  I finally found the solution in this post. I commented out the system.diagnostics  section in the web.config and ta-da it worked!

Probably coming up in Part 2: The design. Because at the time I write this post the blog looks a bit to general. But I warn you on beforehand, I'm a technician and not (and I repeat) NOT a designer!

November 8, 2011 - Comments [0] - Posted in Blogging
Logging in on a site using apache can be done in a lot of different ways e.g. .htpasswd or mysql. All of them have one thing in common: you get yet another password. What I wanted to accomplish is to be able to login using my normal Ubuntu username/password. This makes it a lot easier to change my password once in a while.

The first step is to install the required applications:
sudo apt-get install libapache2-mod-authz-unixgroup pwauth
Enable the apache module:
sudo a2enmod authnz_external
Edit the appropriate apache site in /etc/apache2/sites-available, make sure the site is only available over SSL otherwise you password will travel over the Internet unencrypted!
AddExternalAuth pwauth /usr/sbin/pwauth
SetExternalAuthMethod pwauth pipe

<location /sickbeard/>
    order deny,allow
    deny from all
    allow from all

    ProxyPass http://localhost:8081/sickbeard/
    ProxyPassReverse http://localhost:8081/sickbeard/

    AuthType Basic
    AuthName "Boaz' Sick Beard"
    AuthBasicProvider external
    AuthExternal pwauth
    Require valid-user
Finally restart apache and you're ready to go!
sudo /etc/init.d/apache2 restart
October 23, 2011 - Comments [0] - Posted in Ubuntu