MonoRail – RenderMailMessage – System.ArgumentNullException: Value cannot be null. Parameter name: format

This was a nasty issue…

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: 
Value cannot be null.
Parameter name: format
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at System.String.Format(String format, Object arg0, Object arg1)
   --- End of inner exception stack trace ---

Background

This is part of the stack trace I got in one of my applications which uses MonoRail. I got it while creating the Castle.Components.Common.EmailSender.Message in order to prepare the content of the email having the name of its template file (vm):

Message msg = RenderMailMessage(templateName)

That view file defined the content and used data from PropertyBag and from Resource files.

Just to recall a resource file (resx) is bound with the controller class with this definition:

[Resource("text", "LocalizationSample.Resources.Home")]

What was wrong there? I was sure templateName passed as the parameter was correct – it for sure pointed to correct vm file. Moreover, that piece of code was defined in a superclass which was extended by this particular controller and another one which also could send this email. Of course there was no problem with sending email by the latter.

Solution

The problem here was I used this construction in the vm file:

$string.format($text.someText, $param1, $param2)

And for some reason I forgot to bind the appropriate resource file (the one referenced with $text) with one of the controllers. As a result string.format failed because $param1 and $param2 couldn’t be injected into string which was not found.

Previous Post
Next Post