Search code examples
wpfmsbuildmsbuild-buildengine

Microsoft.Build.BuildEngine.Engine throws error when building WPF application


I am using Microsoft.Build.BuildEngine.Engine to build a WPF application. This has been working successfully for class libraries and web applications, but now trying to use it to build a WPF application I am getting the following error:

Target MarkupCompilePass1: c:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.WinFX.targets(294,9): error MC1000: Unknown build error, 'API restriction: The assembly 'file:///C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.' Done building target "MarkupCompilePass1" in project "TestWindowsApplication.csproj" -- FAILED.

This application builds fine when building using VisualStudio 2008 (i.e. build from the menu), but using the Microsoft.Build.BuildEngine.Engine it throws this build error. Anyone know what is going on here?


Solution

  • I had the same problem and found this on msdn which says

    By default, markup compilation runs in the same AppDomain as the MSBuild engine. This provides us significant performance gains. This behavior can be toggled with the AlwaysCompileMarkupFilesInSeparateDomain property. The latter one has the advantage of unloading all reference assemblies by unloading the separate AppDomain.

    So since the exception thrown stated that PresentationCore was loaded in the same AppDomain I switched this property using:

    projectToBuild.SetProperty("AlwaysCompileMarkupFilesInSeparateDomain", "True");
    

    Which seemed to be the key.

    I hope this helps.