错误 MSB4801:.NET Core 版本的 MSBuild 不支持任务工厂“CodeTaskFactory”

Son*_*sen 6 c# msbuild .net-6.0

我的项目.Net 6中有一个UsingTask。我有一个小脚本来使用CodeTaskFactory设置AssemblyVersion。请参阅下面的代码。但是,我无法使用 MS Build 在构建服务器上使用或执行 CodeTaskFactory。

它给出了这些错误:

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
Run Code Online (Sandbox Code Playgroud)

我研究了其他一些选择,但都失败了,因为它们缺少一件事或另一件事。所以最后我在这里问,在 MS Build 服务器上运行带有输出的 usingTask 有哪些选项?

我的代码

  <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>
Run Code Online (Sandbox Code Playgroud)

小智 15

错误信息是正确的。您UsingTask正在尝试使用不可用的.Net Framework。更新UsingTask以使用RoslynCodeTaskFactory代替CodeTaskFactory

例如:

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

请参阅MSBuild 内联任务,其中指出(重点是我添加的):

在 MSBuild 15.8 中,添加了 RoslynCodeTaskFactory,它可以创建 .NET Standard 跨平台内联任务。如果需要在.NET Core上使用内联任务,则必须使用RoslynCodeTaskFactory。

另请参阅使用 RoslynCodeTaskFactory 的 MSBuild 内联任务