Unable to create inline build task using CodeTaskFactory (build error MSB3758 and CS0012)

873 views Asked by At

I have a WiX project in Visual Studio 2010 that uses an inline MSBuild Task to set an environment variable in the "BeforeBuild" Target. The custom task previously built with no problems; however, I am now getting build errors, even though the project/solution has not changed at all since the last successful build. In fact, the same code builds correctly on another developers machine...

If I build the project from the command line, it also builds correctly. This leads me to believe that the VS 2010 environment may be somehow corrupted?

Since last building the project, I have installed a number of programs (VS 2015, .NET Core, etc). Would repairing VS 2010 be an option? I have VS 2010 SP1 installed.

The custom task is defined inline in the project file:

<!-- Define task to set environment variables for the current process -->
<UsingTask TaskName="SetEnvironmentVariableTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll">
  <ParameterGroup>
    <Name ParameterType="System.String" Required="true" />
    <Value ParameterType="System.String" Required="true" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System" />
    <Code Type="Fragment" Language="cs"><![CDATA[
      Environment.SetEnvironmentVariable(Name, Value, EnvironmentVariableTarget.Process);
    ]]></Code>
  </Task>
</UsingTask>

And is called in the before build target:

<Target Name="BeforeBuild">
  <SetEnvironmentVariableTask Name="WIX_TEMP" Value="$(MSBuildProjectDirectory)\$(IntermediateOutputPath)" />
</Target>

The build error is:

Initializing task factory "CodeTaskFactory" from assembly "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll".

error MSB3758: An error has occurred during compilation

error CS0012: The type 'Microsoft.Build.Framework.ITask' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

UPDATE 2017-01-19

I tried using Emilio's suggestion below; however, it seems like the MSBuild variables are not being correctly set (I used a <Message> element to output them during the build process):

MSBuildExtensionsPath = 'C:\Program Files (x86)\MSBuild'

MSBuildAssemblyVersion = ''

MSBuildToolsPath = 'C:\Windows\Microsoft.NET\Framework\v4.0.30319'

Also, if I look in the C:\Program Files (x86)\MSBuild folder, I only see sub-folders for VS 2013 (12.0) and VS 2015 (14.0), not for VS 2010 (10.0)... Although I'm not sure if that's where VS 2010 would install MSBuild stuff?

1

There are 1 answers

4
Emiel Koning On

It seems that installing VS2015 modifies some of the MSBuild variables you're using (e.g. $(MSBuildToolsPath)).

I had the same problem when using

<UsingTask TaskName="MyCustomTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">

regardless of the ToolsVersion specified in the project file.

I ended up using

<UsingTask TaskName="MyCustomTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildExtensionsPath)\$(MSBuildAssemblyVersion)\Bin\Microsoft.Build.Tasks.Core.dll">