Archive for April, 2010

Deploying .NET 4 Project – Error 1001 (System.BadImageFormatException)

After attending the UK Tech Days events last week in London, I was keen to jump on the Visual Studio 2010 and .net 4.0 bandwagon.

I converted some of our projects here at Crocus to the .net 4 framework (which was incredibly easy – nothing broke!)
I even took advantage of some of the quick to implement features in .net 4, and converted some of our massively over-ridden methods to use optional parameters.

One project in particular is a Windows Service, that sends out purchase orders on a schedule.
(I recently wrote about how this broke due to Quartz.net expecting a UTC start time)

This has a Visual Studio deployment project associated with it.

After building the newly upgraded .net 4 version of the project, and deploying the .msi file to our target server, I got the following error:

Error 1001 Exception occurred while initializing the installation. System.BadImageFormatException: Could not load file or assembly or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Now, I had definitely upgraded said server to .net 4 (twice, as a matter of fact – after the first time i received this error!)

After some Googling, some people were saying to change the platform target on my assemblies, which i did, to no avail. 

I eventually discovered the problem.

You need to set the .NET Framework Launch Condition

Here’s how to do it:

Right click on your deployment project in solution explorer

Right click on your deployment project in solution explorer

Under “Version” Choose .NET Framework 4

Under “Version” Choose .NET Framework 4

After rebuilding and deploying my setup file, everything worked fine.


C# 4.0 – Optional Parameters

I know, I know, VB has had them for ages.

But I don’t care… Now C# has them too!

This will greatly reduce method overloads!
Check this bit of code (sorry it’s not a “real world” example)

public class TestClass
{
    public void DoSomething(string parameterOne, string parameterTwo, string parameterThree)
    {
        DoSomething(parameterOne, parameterTwo, parameterThree, null);
    }

    public void DoSomething(string parameterOne, string parameterTwo, string parameterThree, string parameterFour)
    {
        if (parameterFour != null)
        {
            //doing somthing with the parameters.
        }
    }
}

The DoSomething method has an overload on it that takes a fourth parameter (in this case parameterFour)

When we use the above code, our intellisense looks like this:

 

overload1

…and the second overload

 

overload2

As we can see, the method has 2 overloads. One taking parameterFour, one not.

New in C# 4.0, we can use something called optional parameters.

This allows us to change our method to:

public class TestClass
{
    public void DoSomething(string parameterOne, string parameterTwo, string parameterThree, string parameterFour = null)
    {
        if (parameterFour != null)
        {
            //doing something with the parameters.
        }
    }
}

As you can see, we’ve removed the overloaded method – I’m sure you can appreciate, if we had a method with many overloads, this results in much cleaner code :-)

Now, our intellisense looks like this:

intellisense-with-optional-parameter-method

In my opinion, this looks a bit confusing.

Sure, I’ll get used to it, but in my opinion, the method is still sort of overloaded… just with optional parameters!

Quartz.net trigger not firing

I’ve used Quartz.net for a little while – more specifically, around 6 months. I started working with it around the end of October 2009. Irrelevant, you may think, but the important thing here is the time. During winter months, the UK runs on GMT (or UTC+0)

During these months, (up until 28th March 2010) my application functioned as expected – however, when the clocks went forward for British Summer Time, the trigger stopped firing.

The problem?

I’d specified my triggers start time like this:

trigger.StartTimeUtc = DateTime.Now;

Which was fine, when, before the clocks go forward for summer.

After the clocks go forward (and we start enjoying lighter evenings etc…) the UK is on UTC+01 (British Summer Time)

This means, at the time of writing (circa 14:45pm, April 12th 2010) the values of DateTime.Now and DateTime.UtcNow are as follows:

DateTime.Now

12/04/2010 14:47:39

DateTime.UtcNow

12/04/2010 13:46:58

… a whole hour and a few seconds difference.

By using DateTime.Now to set the start time (which expects the time as UTC) – I was in fact, telling Quartz.net to start the trigger at 14:47 – an hour later than I wanted.

Summary

Always use DateTime.UtcNow to set the Triggers StartTimeUtc :-)