Cloudflare DDNS Updates (Aug 2018 Edition)

In one of my old blog posts I covered some scripts to update Cloudflare’s DNS system. This is useful if you’re hosting sites at home on a Raspberry PI (like this one still is!), where you might not have a static IP.

In the intervening 3 years, the API has changed, so I’ve revisited the scripts to update them to the new shiny “Cloudflare API V4”. The updated scripts are tested on the current latest image of Raspbian Stretch at the time of writing. The new scripts are available on GitHub here.

The structure of the scripts is still the same:

  • is the main update script, intended to be setup to run automatically via crontab or similar.
  • is used to read the host id value from the host record that needs updating (while Cloudflare mention some API details and stubs of requests in the API section of the DNS dash page, there still doesn’t seem to be a nice way to get this id. Its just mentioned as “:identifier” in those stubs).

If they break again and I don’t notice, or if there are any problems with the scripts, please raise an issue on the Github Repo.

Overriding Modernizr

Modernizr is a great tool for browser feature detection, but there are times when it’s useful to override Modernizr’s detection and force the configuration of features that are available, both on the Javascript object and also the CSS marker classes. An example of this is when using Chrome Dev Tools to write fallback functionality for older mobile devices – Windows Mobile 7.5 and a Galaxy Ace with Android 2.3 have been problems for me lately.

To help with this I wrote a simple modernizr-override helper to take a predefined Modernizr configuration and inject it on top of the detected features in Chrome.

To get it, and see how to use it, head over to the Github repo here.

Confusion around VirtualBox CustomVideoModes in Windows

There are plenty of instances in online forums (eg here and here) of questions about setting CustomVideoModes in VirtualBox when running Windows as the client OS.

What is CustomVideoMode?

A CustomVideoMode is an additional screen resolution made available to the VirtualBox guest OS over and above those that are already present by default in VirtualBox.

The command to set a CustomVideoMode in VirtualBox is:

VBoxManage setextradata "VM Name" CustomVideoMode1 1920x1080x32

This command is run from your VirtualBox installation location in the host OS. The VM Name must be the name of your virtual machine, CustomVideoMode1 can change to CustomVideoMode2 etc to allow for additional extra modes, and the resolution part should be set to the resolutions you need.

What actually happens in Windows

This isn’t actually the proper way to deal with resolutions in Windows guests. Instead, the Guest Additions should be installed into the client OS, and then the option to use Auto-resize Guest Display will resize the guest to whatever size it’s containing window is at. In fact, if you use the above command to add custom video modes, and then look at the options in the display properties on the guest, then they won’t even be shown – usually there will just be the VirtualBox defaults plus whatever size the guest happens to have sized itself to at that moment.

In my recent experience however this isn’t the full story here. I often find that, on my running virtual machines, the auto-resize stops working, particularly after the host sleeps, or the number of monitors attached to the host changes (I use a laptop most of the time, and plug into a dock at home).

What’s going on?

VirtualBox Guest Additions installs a VirtualBox Display adapter driver – this is what handles the auto-resize for us. Unfortunately, I find that this crashes and gets disabled by the guest OS fairly frequently. I can see errors in my guest event log such as:

Display driver VBoxVideoW8 stopped responding and has been successfully disabled.

I suspect I’m getting this more than most as I’ve been running the preview builds of Windows 10 for quite a while in my guests, and my use of a laptop means that my display switches relatively often.

As the message says the VBoxVideoW8 driver is disabled. This is what stops the resizing from working. Instead we fallback to using the Microsoft Basic Display Driver, and we’re stuck with the original defaults. When the guest OS is rebooted the VirtualBox driver will usually work again, but rebooting is usually undesirable as our current workspace is lost. Alternatively we can try to reinitialize the VirtualBox driver by going through the Update driver dialogs and manually selecting the Oracle Corporation >> VirtualBox Device driver. Unfortunately I find that this tends to require a reboot to take effect anyway.

CustomVideoMode to the rescue

Helpfully, the Microsoft Basic Display Driver does pick up settings made using the CustomVideoMode. This means we can have a nice fallback when the VirtualBox driver stops. Adding our normal working resolutions lets us manually switch between then as required in the guest display properties dialog.

Therefore I tend to install my Windows VMs with the following two statements that cover my two main display resolutions:

VBoxManage setextradata "VM Name" CustomVideoMode1 1920x1080x32
VBoxManage setextradata "VM Name" CustomVideoMode2 1366x768x32

With these set, I can easily switch the guest to work nicely in full screen on whichever display I need without an annoying reboot or mis-sized guest.

CloudFlare DDNS updates

This blog is hosted on a Raspberry Pi under my TV at home. As is common with this scenario I have a dynamic WAN IP, updating intermittently at the whim of my ISP/router.

After a few attempts I’ve finally got a stable DDNS update that works with CloudFlare (having had trouble with various bits of ddclient+patches etc) in the form of some scripts that call curl against the Api directly. This seems nice and neat, and the scripts can be scheduled using cron.

I’ve posted the scripts used here. I’ll try to keep these updated as the CloudFlare Api changes (as I’ll have to to keep the site running!).

I’ve included the script required to download details of the dns records from CloudFlare, as this is required to get the rec_id value for the dns entry, which is then sent back for the update. My version of the main update script also maintains a simple log file of updates.

The repo is here, the update script is here
and the read script is here

A TinyIoC Bootstrapper

I really like the TinyIoC Inversion of Control Container. It’s nice and straight forward to use, it’s easily portable, and the auto registration feature does 99% of your work for you. In recent projects it has been an ideal choice, either because of the size of the project, or to introduce the concepts of DI to teams that weren’t already familiar with it.

With a little work though we can help it punch a little above its weight.

The sidekick to an IoC container is the Bootstrapper. A good bootstrapper can make DI a simple painless exercise that just works, so I thought I’d share mine. It started life with the DefaultNancyBootstrapper from the Nancy project and evolved from there.

It’s available and documented with examples on GitHub here, so I won’t go into it in depth, but I will cover the main aspects I wanted to solve.

Auto Scanning

The DefaultNancyBootstrapper auto registration takes all types from AppDomain.CurrentDomain.GetAssemblies. In some scenarios though this isn’t great, as it only picks up those assemblies that are already loaded. As the bootstrapping is usually done very early in the life of the application it may easily be that the bootstrapping omits many of the types. Any missed then need to be hooked up manually. I wanted to enhance this scanning to require less help.


The bootstrapping needs to be flexible enough to allow the developer control over which assemblies/types are not scanned, and to pass in extra assemblies for scanning which aren’t available for pickup automatically.


The bootstrapping should be configurable across various layers in a more complex system (so maybe a solution wide base bootstrapper, and then specific overrides for MVC/WebApi/Service projects).

After a few iterations I ended up with a nice lightweight solution, which had the configurability I was looking for.

You can see the whole project here, or jump straight to the bootstrapper class here.

SignalR: The ConnectionId is in the incorrect format – an alternative cause.

Recently, on an application using SignalR, we found that we were getting many System.InvalidOperationException: The ConnectionId is in the incorrect format exceptions (sometimes 10 per second) from the SignalR hubs in the site.

While there is plenty of discussion on the causes of this error in SignalR documentation and on Q&A sites, they mostly discuss problems with changing levels of authentication (SignalR defines a new connection when the auth level, or authorized user, changes). In this case though this wasn’t relevant. The volume implied that this was unlikely to be the cause, and in anycase, the site was already written to cope with these changes on login.

The cause turned out to be search engine crawler services. Continue reading SignalR: The ConnectionId is in the incorrect format – an alternative cause.

ASP.Net Webforms – Dynamic UpdatePanels and UserControls issue

Have come across an interesting issues in ASP.Net WebForms when migrating a project from 3.5 up to 4.5.

The site in question is extremely dynamic, the page is built up based on configuration in a CMS fashion.

However in 4.5 we have a problem – when more content is added into the page via a button click, not all the markup for the content appears.

Continue reading ASP.Net Webforms – Dynamic UpdatePanels and UserControls issue

Techniques for cleaner dynamic SQL generation

Very often when working in databases we have to resort to dynamic SQL generation to solve a problem. For example, common instances of this would be when performing an action across a number of different tables, or performing a PIVOT on a data set where the set of pivot results is not pre-determined. This latter example is one which I’ll take through this walk through.

The scenario is that we have a table of Person data and a table of Payslip data, and the requirement is to return each Person with the salaries listed out year on year. Its a simple example, but not unlike a real-world problem.

Continue reading Techniques for cleaner dynamic SQL generation

AutoMapper and EntityFramework Proxies – a workaround

This is a quick workaround for an issue I came across when working with AutoMapper on EF. Its one of those blog posts that’s as much a reminder for the writer as anything else.

I was trying to repopulate an entity instance from a corresponding model instance using a line similar to the following:

Mapper.Map<MyModel,MyEntity>(model, ent);

At this line I got an AutoMapper.AutoMapperMappingException accompanied by the following message:

Missing type map configuration or unsupported mapping. Mapping types: MyModel-> MyEntity_238F6DF9C0DAD0768B6BF2E9… MyProject.MyModel-> System.Data.Entity.DynamicProxies.MyEntity_238F6DF9C0DAD0768B6BF2E9…

Take as read that I certainly did have a Mapper.Create<MyModel,MyEntity>() type line in place.

Continue reading AutoMapper and EntityFramework Proxies – a workaround

DropDownList Helper For Enums

Its useful to be able to have a helper that converts a Enum typed property from an MVC Model directly into a drop down list without any messing about creating lists of possible values etc.

I found this answer on StackOverflow which has example code for just such a helper: EnumDropDownListFor. However, like a few who have tried it I found some issues around getting it to work to pre-select the value held in the Model’s property.

Continue reading DropDownList Helper For Enums