Building .Net projects with Nant – Unable to find sdkInstallRoot keys

A quick but incredibly annoying issue.

I upgrade my machine recently to use Visual Studio 2008 so that I could upgrade a .net 1.1 project to .net 2.0. As I mentioned in the last post, this has caused a great deal of problems and a little bit of fodder for this blog. This issue however, takes the cake.

I had to upgrade to the new version of nant, which we use to perform command-line building on our continuous build server. Unfortunately nant does not support VS2008 solutions files, only 2003-2005, despite the fact it supports building to .Net 2.0 which is what I needed to do. This was easily gotten around by using exec to call msbuild and compile using the microsoft command line tool.

I could have converted the entire thing to msbuild, however the nant script does a lot more than just compile, it runs tests and does precompilation copying and generating that would be painful to move. Also it needs to play nicely with an ant script that originally calls it, which nant does quite well.

The issue occured however when I set nant to target .Net 2.0. Strictly speaking I didn’t have to do this, as msbuild took care of it when checking the solution file, however I like things to be tidy and didn’t want nant targeting .Net 1.1 when we’re building for 2.0.

It crashed almost immediately when started claiming it couldn’t find the sdkinstallroot key, the key that determines where on the file system the relevant SDK’s can be found (in order to link against them). I checked the registry and indeed, no sdkInstallKey existed for any version other than 1.1 despite the fact that VS2008, when installed, targets .Net 2.0, 3.0 and 3.5.

It turns out that VS2008 targets these frameworks by understanding that both 2.0 is a subset of 3.0, which is a subset of 3.5 – each new version has expanded on the previous, rather than replacing it or changing the interface. Thus, it can build for 2.0 simply by leaving out links to all parts of the framework which don’t exist in 2.0.

What this means for us though is no sdkInstallRoot… in real terms, no sdk at all.

The solution is as simple as the final explanation, simply install the microsoft SDK for the version you’re targeting and it’s as right as rain. When the sdk is installed, the key is added and nant finds it exactly as it expects to – it just means you need to install a copy of all the sdk’s you need alongside the visual studio built-in libraries.

System.NotSupportedException : Cannot dynamically create an instance of System.Void

I came across an interesting bug during a major .Net 1.1 – .Net 2.0 upgrade the other day which took a bit of searching and showed what appears to be a fairly obscure bug in the .Net 1.1 libraries.

Our .Net project made use of the EasyMock .Net libraries in order to perfrom mock-tests using NUnit both to check our own work, and on the continuous build server to ensure that builds we release for testing are all they can be.

Updating to .Net 2.0 I noticed that EasyMock .Net itself didn’t have a later version designed for the new system, and running the old one was coming up with some obscure errors in the tests – the NotSupportedException shown on the title of this post.

A bit of investigation showed that EasyMock, when acting as a proxy for a method call, creats an instance of the return type of that method call. In the case of Void methods it attempts to create an instance of System.Void using Activator.CreateInstance().

According to the MSDN libraries for .Net 1.1, it is -never- possible to create a dynamic instance of a System.Void type. This line should never have worked for values of void, but no matter where I looked I couldn’t see why we got to that position in .Net 2.0 and not in .Net 1.1 – there was no conditional for Voids that should have shunted us on to a different track, nor any special handling for same.

Finally I booted up an old copy of the source in Visual Studio 2003 and traced through in there and lo-and-behold! It successfully created a dynamic instance of a System.Void type. For some reason, despite documentation to the contrary, it was able to do this in .Net 1.1.

It can’t in 2.0 or later, so if your code tries to do this I would suggest finding a different way to accomplish your goals, otherwise remain version-stuck and unable to update.

Also, Easymock .Net appears to be defunct, and in any later environment completely useless without an overhaul and careful examination of the source for other such unsupported ‘tricks’ that the developers may have used, either knowingly or without realising. I ripped it out of our system and replaced it was nmock2. It also hasn’t been updated past .Net 2.0, but it’s a step up.

Did require a complete test rewrite however. It was a pain.

NUnit and STAThread

Heres an interesting one I ran into today:-

When you access OLE calls from .Net in .Net 2.0 or greater, you need to do it from an application where the main thread is set to Single Threaded Apartment (as opposed to Multile Threaded Apartment). What that means, i’m not entirely sure and didn’t spend the time to look in to i’m afraid.

This becomes an issue as the newer versions of NUnit are set to MTA by default – so when I updated an application from .Net 1.1 to .Net 2.0 and used the new NUnit, previously passing tests failed with the error – “System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.”

There is a simple way around this, as I found on CodeCentric (http://codecentrix.blogspot.com/2007/12/nunit-and-stathread-story.html) – simply create an app.config file for the dll you are attempting to test, and add some NUnit appropriate settings to force NUnit to create the test environemnt as STA instead of MTA.

For convenience sake, here is the config file you would need (or add these sections to your existing config file):

<?xml version=”1.0″ encoding=”utf-8″?>
<configuration>
<configSections>
<sectionGroup name=”NUnit”>
<section name=”TestRunner” type=”System.Configuration.NameValueSectionHandler”/>
</sectionGroup>
</configSections>

<NUnit>
<TestRunner>
<add key=”ApartmentState” value=”STA” />
</TestRunner>
</NUnit>

</configuration>

Google Chrome

Whilst the whole world is going batshit over Google’s surprise new entry into the browsing market, just a few quick comments.

– It’s nice and clean, looks pretty and has some very interesting ideas (process per tab, internal process manager etc).

– It doesn’t support any kind of extension framework a-la firefox, and so is unlikely to be a candidate for full-time switching yet for many people who are firefox users. This is strange as these people seem to be Google’s target demographic, given the amount of words spent describing shiney tech features such as the seperate-process-space-per-tab feature.

– In the very short (20 mins) time I spent playing with it, it failed to display quite a few pages properly. Amongst them, Google Reader and Google Account Login. They’re not kidding this time when they say “Beta”.

– It might be somewhat disturbing to see the slight meshing of typical OS functionality and browser functionality, and the way Chrome is encroaching slightly onto space typically thought of as OS controlled. This might say something about the Google philosophy.

– It’s early days yet. Going forward, Google have the ability and resources to make a major grab in this area. I’m sure saying that both Mozilla and Microsoft are paying attention would be an understatement.

– Back on the pc for this post, and it’s far quicker and more comfortable than writing them on an iPhone. iPhone post functionality is likely best for short posts when away from the desk.

iPhone Applications

Let’s start with something good about the iPhone. Applications.

Many of the apps in the iTunes app store are trite and stupid – but that’s true of most development platforms with a fairly low barrier of entry and not overly surprising. Some apps however are excellent and are themselves possibly the best ( or only ) reason to recommend the platform.

First, WordPress. I feel I must mention this app as I’m currently using it to write this post and it’s clean, easy to use, and makes use of the iPhone’s native keyboard and predictive text which is quite pleasant for long posts like this one.

iPhone’s text abilities are also very useful in the application ‘Frotz’. There is a small but committed interactive fiction community still alive on the Internet and long time readers will remember I’ve mentioned them on many occasions in the past. Frotz for the iPhone allows you to play any z code game on your phone and provides the ability to download games direct from the ifdb site. The only thing missing is a bonjour server for pc to load up z code files you already possess.

I mention that because it is exactly what BookShelf offers to get your ebooks onto your phone to read. BookShelf itself justifies my iPhone purchase as it is a fantastic ebook reader that shows off well the iPhone’s excellent screen resolution. It’s not perfect as it’s chunking is annoying and it crashes occasionally but on the whole it is worth the 12$ au price tag. This is so far the only non-free app that holds any interest for me.

So there’s a couple of great things about the iPhone, but it’s not all roses and teddy bears. The platform is reasonably unstable and crashes are not unusual, my last post illustrates the worst of that, and I am still irritated by the fact I’ll need to buy a licence if I wish to give iPhone development a try.

Still, things aren’t too bad on the application front. If you’re an iPhone user yourself, let me know which of your favorite apps I’ve missed.

Now let’s see if this app will post well.

iPhone 3g Appstore apps constantly crashing

http://www.appleiphoneschool.com/2008/08/10/appstore-apps-crashing-restore/

And now for something completely different…

It’s been a while since my last post and I thought i’d get away from C# and Java goodness for a while, and talk about something new.  The iPhone.  I just signed up for one a few days ago and have been playing around with it all weekend.

As many people will, i’m sure, I immediately hit the app center and grabbed a bunch of free apps, toys and even bought an app (Bookshelf, an excellent, though slightly buggy in a few points, eReader program).
Well, about twenty minutes ago I discovered that one of my apps, Easy Task Manager, would no longer open, instead it crashed straight back to the home screen.  It didn’t take long to discover that suddenly -all- of my app-store apps were now doing exactly this.  I searched for some sanity and found the link above.
There are an awful lot of possible solutions presented in the above link, from full restoring to jailbreaking and resetting components, to sending your iPhone back to apple.  One that you might miss whilst looking through the comments however is the one that fixed the problem for me.  Simply go into iTunes, choose “Authorize Computer” from the Store menu, and enter your iTunes password.  That will go through some sort of authorisation process, and after that run the sync command.  (Do all this with the iPhone attached).

Don’t ask me why that worked, it really does stretch the imagination – but then, so do a lot of things about Apple and the iPhone.  Don’t get me wrong, it’s a nice little toy, but there are a lot of irritating little things about it.  Expect to hear more in the coming days, it’s a nice new little subject to rant on before I try to get back into our previously regular scheduled programming 🙂

Thanks for tuning in once more, come again and good coding.