static void

Build Test

This is a test to build a project which has code generation (the generation is omitted here). The core code is based on Msdn, to programmatically build projects, useful in other scenarios. For Visual Studio 2008 I had to do some things that weren't in the Msdn.

//#define NET4
using System;
using System.IO;
using Microsoft.Build.BuildEngine;
#if NET4
using Microsoft.Build.Evaluation; //reference Microsoft.Build.dll v4.0
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Testing
    /// <summary>
    /// Tests a project builds correctly
    /// </summary>
    public class TestBuild
        [DeploymentItem(@"Project", @"Project")]
        public void BuildProject()
            var workingDirectory = Path.Combine(Environment.CurrentDirectory, @"Project\");
            var csproj = Path.Combine(workingDirectory, @"MyProject.csproj");
            if (!File.Exists(csproj))
                Assert.Inconclusive("Sources were not deployed");
        /// <summary>
        /// Builds the project - based on Asserts success.
        /// </summary>
        /// <param name="projectPath">The project (csproj) path</param>
        /// <returns>True if builds okay</returns>
        private static bool BuildProject(string projectPath)
            var logPath = Path.Combine(Path.GetDirectoryName(projectPath), "build.log");
#if NET4
            //.Net 4 Microsoft.Build.Evaluation.Project and ProjectCollection
            var engine = new ProjectCollection();
            // Instantiate a new Engine object
            var engine = new Engine();
            // Point to .NET Framework 2.0 CLR (not needed in 3.5 apparently)
            //engine.BinPath = RuntimeEnvironment.GetRuntimeDirectory();
            // Instantiate a new FileLogger to generate build log
            var logger = new FileLogger();
            // Set the logfile parameter to indicate the log destination
            logger.Parameters = @"logfile=" + logPath;
            // Register the logger with the engine
            // Build a project file
#if NET4
            bool success = engine.LoadProject(projectPath).Build();
            bool success = engine.BuildProjectFile(projectPath);
            //Unregister all loggers to close the log file
            //if fails, put the log file into the assert statement
            string txt = "Should have built";
            if (!success && File.Exists(logPath))
                txt = File.ReadAllText(logPath);
            //assert succeeded
            Assert.IsTrue(success, txt);
            return success;

App.Config with binding redirect

<?xml version="1.0" encoding="utf-8" ?>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion=""