What does Entity Framework has to do with MSBuild?

November 24, 2008

13 comments

The answer to the above question should be “nothing, unless you’re trying to build a project that references entity framework”, but apparently it isn’t so.

Say you have a project you’ve built, and you want to build it through code, using the Engine class of Microsoft.Build.Engine assembly.

The code should look something like this (taken from MSDN):

// Instantiate a new Engine object
Engine engine = new Engine();

// Instantiate a new FileLogger to generate build log
FileLogger logger = new FileLogger();

// Set the logfile parameter to indicate the log destination
logger.Parameters = @"logfile=C:\temp\build.log";

// Register the logger with the engine

engine.RegisterLogger(logger);

// Build a project file
bool success = engine.BuildProjectFile(@"c:\somewhere\someproject.csproj");

//Unregister all loggers to close the log file
engine.UnregisterAllLoggers();

if (success)
    Console.WriteLine("Build succeeded.");
else
    Console.WriteLine(@"Build failed. View C:\temp\build.log for details");

Some notes about the code:

  1. If you refer to the sample code given in the MSDN: http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.engine.registerlogger.aspx, you can drop the line that sets the BinPath – the property is obsolete, but for some reason remained in the sample code.
  2. Don’t forget to add both Microsoft.Build.Engine and Microsoft.Build.Framework assemblies to your code (both in the 3.5 version)

When you try to run the code on a project with a target framework of 3.5, you might get an error like this in your log file (‘I’ve marked the important stuff in bold):

Target EntityDeploy:

    C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.targets(40,5): error MSB4127: The "EntityDeploy" task could not be instantiated from the assembly "C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type ‘Microsoft.Data.Entity.Build.Tasks.EntityDeploy’ to type ‘Microsoft.Build.Framework.ITask’.

    C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.targets(40,5): error MSB4060: The "EntityDeploy" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

Done building target "EntityDeploy" in project "SampleClassLibrary.csproj" — FAILED.

If you look at the Microsoft.Data.Entity.Build.Tasks assembly, you will notice that this assembly uses both versions of Microsoft.Build.Framework – both version 2.0 and 3.5

image

Of course, if you try to add both versions to your project, it just won’t work, because both have the same name.

So, how can we make it work? just let the application know that it should always use version 3.5, even if version 2.0 is specified (implied in the error message). To do that, you need to add an assemblyBinding to your config file:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Build.Framework"
publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>

 

And that’s it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

13 comments

  1. GregJanuary 9, 2009 ב 11:44 pm

    Thank you so much This really helped me out

    Reply
  2. BlahOctober 12, 2009 ב 2:27 pm

    Great! Another hard coded value for some nebulous config file.

    Reply
  3. jcollum@gmail.comApril 6, 2010 ב 11:50 pm

    Good post, but I’m still having this issue. See this post if anyone else runs into this problem: http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/468d3ff5-74a9-4100-a7e4-5e71e74f57ad/: . .

    Sorry for typos. This RTL stuff is acting weird.

    Reply
  4. Justin CollumApril 6, 2010 ב 11:55 pm

    Thanks for the post. Unfortunately it didn’t solve the same issue for me.

    I posted on MS forums about it: http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/468d3ff5-74a9-4100-a7e4-5e71e74f57ad/

    (this may be a double post, sorry).

    Reply
  5. john@john.johnJuly 28, 2010 ב 3:56 pm

    Thanks a lot you saved me from another headache

    Reply
  6. Scott BrooksNovember 8, 2010 ב 12:08 am

    Wow, thanks for taking your time to do such a perfect article!

    Reply
  7. Adriano GonçalvesNovember 10, 2010 ב 1:18 pm

    Thank you so much! It worked fine for me… Remering that puting the *publicKeyToken=”b03f5f7f11d50a3a”* is essential to the solution work right.

    Regards

    Reply
  8. KhaniyaDecember 3, 2010 ב 9:05 am

    Sorry not work for me
    you said there may be multiple reference of same type. But I have added only 3.5 version of framework

    Note : how can I add different version of same Framework. It gives me error like “It is already exist”

    Thanks

    Reply
  9. KhaniyaDecember 3, 2010 ב 9:05 am

    Sorry not work for me
    you said there may be multiple reference of same type. But I have added only 3.5 version of framework

    Note : how can I add different version of same Framework. It gives me error like “It is already exist”

    Thanks

    Reply
  10. Daniel CarneiroMarch 2, 2011 ב 1:06 pm

    Thanks for your post. It worked like a charm

    Reply
  11. Subhash ChandJuly 19, 2011 ב 9:10 am

    Thanks. It really help me.

    Reply
  12. Ritesh jhaApril 12, 2013 ב 12:36 am

    thanks a lot…from last 4 hours i was struggling.
    thank u so much.

    Reply