Sonnich Jensen
Sonnich Jensen

Reputation: 253

Error MSB4801: The task factory "CodeTaskFactory" is not supported on the .NET Core version of MSBuild

I have a UsingTask in my project, .Net 6. I have a small script to set the AssemblyVersion, using CodeTaskFactory. See code below. However, I cannot use or execute CodeTaskFactory on the build server using MS Build.

It gives these errors:

Error MSB4801: The task factory "CodeTaskFactory" is not supported on the .NET Core version of MSBuild
Error MSB4175: The task factory "CodeTaskFactory" could not be loaded from the assembly "C:\Program Files\dotnet\sdk\6.0.101\Microsoft.Build.Tasks.Core.dll". The task factory must return a value for the "TaskType" property

I looked at some other options, which all failed as they lacked one or another thing. So finally I ask here, what are my options to run a UsingTask with an output on the MS Build server?

My code

  <UsingTask TaskName="mytask"    
      TaskFactory="CodeTaskFactory"
      AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <AssemblyVersion ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
          Log.LogMessage(MessageImportance.High, "we are here");
          AssemblyVersion = "1.2.3.4";
        ]]>
      </Code>
    </Task>
  </UsingTask>

Upvotes: 8

Views: 7629

Answers (1)

Jonathan Dodds
Jonathan Dodds

Reputation: 5163

The error message is correct. Your UsingTask is trying to use .Net Framework which is not available. Update the UsingTask to use the RoslynCodeTaskFactory in place of the CodeTaskFactory.

e.g.:

<UsingTask
    TaskName="mytask"
    TaskFactory="RoslynCodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >

See MSBuild inline tasks which states (with emphasis added by me):

In MSBuild 15.8, the RoslynCodeTaskFactory was added which can create .NET Standard cross-platform inline tasks. If you need to use inline tasks on .NET Core, you must use the RoslynCodeTaskFactory.

See also MSBuild inline tasks with RoslynCodeTaskFactory.

Upvotes: 20

Related Questions