I say.

A blog by Joakim Beijar, I solve interesting and mundane problems at Mios Ltd and hack at night on github.com/kaa.

Custom ResourceLoaders in NVelocity

09 Sep 2008

I needed a custom resource loader for NVelocity to enable me to unit test my new NVelocityView for the ASP.NET MVC app I was building. I studied some examples and went to work. Easy-peasy, I soon had a MemoryResourceLoader that loaded that looked something like this:

public class MemoryResourceLoader : ResourceLoader {
  static IDictionary templates = new Dictionary();
  public static IDictionary Templates { get { return templates; } }
  public static void AddTemplate(string name, string template) {
    templates[name] = Encoding.UTF8.GetBytes(template);
public override Stream GetResourceStream(string template) { byte[] templateBytes; if(!templates.TryGetValue( template, out templateBytes)) { throw new ResourceNotFoundException(
"MemoryResourceLoader Error: cannot find resource " + template); } return new MemoryStream(templateBytes); } public override void Init(ExtendedProperties configuration) {} public override long GetLastModified(Resource resource) {
return 0;
} public override bool IsSourceModified(Resource resource) {
return false;
} }

Which I tried to get NVelocity to use by going:

VelocityEngine engine = new VelocityEngine();
ExtendedProperties properties = new ExtendedProperties();
properties.AddProperty("resource.loader", "memory");

But no! All I got was:

Problem initializing template loader: NVelocityView.MemoryResourceLoader
Error is: System.ArgumentNullException: Value cannot be null.
Parameter name: type

I threw Process Monitor at it trying to figure out if it was looking for my assembly in a bisarre place, I tried fuslogvw.exe to see if there was a problem loading the assembly, I debugged and couldn’t find a solution. Until I decompiled with Reflector and noticed the somewhat strange line

loaderClassName = loaderClassName.Replace(';', ','); 

in the decompiled source. Why would someone use ';' in a type reference? Someone porting from Java of course with some strange configuration framework that uses ',' as a separator between values of course! As soon as I switched ',' to ';' in the initialization code it was all dandy again.

On Sql Server 2008 Express in User Instance mode

15 Aug 2008

When faced with

Failed to generate a user instance of Sql Server due to a failure in starting the process for the user instance

when connecting to Sql Server 2008 Express from Visual Studio, make sure the account running Sql Server has access to the following directory

C:\Users\<UserName>\AppData\Local\Microsoft\Microsoft SQL Server Data

R-Trees - a useful datastructure in this time of the spatial-web

26 Jul 2008

R-Trees - a useful datastructure in this time of the spatial-web

A common real-world usage for an R-tree might be: “Find all museums within 2 miles of my current location”.

On .config files and NUnit, once more

17 Jul 2008

Everytime I need to provide my test cases with a .config file my brain freezes over and I completely forget how I did it last time. Of course I google for “nunit solution config files” and I get lots of unhelpful answers. In particular the suggestion that …

If you use NUnit’s Visual Studio support to load a Visual Studio project or solution, such as mytests.csproj or mytests.sln, the config file must be named mytests.config.

… which is plain wrong. So I move around the .config file with all possible combinations of paths and assembly names to no avail. Finally I fire up trusty old Process Monitor and of course find out that what NUnit is REALLY looking for is mytests.sln.config . From there it’s a slap on the head and plain sailing.

Handling Control-C with finalizers in the CLR

24 Jun 2008

Yesterday was spent debugging an online-banking payment verification service for one our our customers. After a while I found that my test data was running out even though it was supposed to start from the beginning with every round I ran the code. As it turns out the problem was that locks were not beeing freed even though they were supposed to be guaranteed by try { } ... finally { } blocks.

Anyway, so now I find out that finalizers are not called when exiting due to Control-C, and a number of other reasons too. Fortunately there is a solution provided in the previous link, Jolly!