在.Net核心项目构建过程中,ITask无法加载

问题描述:

我有一个相当简单的类,它继承自ITask,并作为更新版本的构建任务运行(VersionUpdater.dll)。该项目文件条目如下:在.Net核心项目构建过程中,ITask无法加载

<UsingTask TaskName="VersionUpdater" AssemblyFile="VersionUpdater.dll" /> 
<Target Name="BeforeBuild"> 
    <VersionUpdater /> 
</Target> 

这对于常规的.Net项目是完全正常的;然而,我试图加载它的.Net核心项目建设任务,并得到这个:

Severity Code Description Project File Line Suppression State Error MSB4062 The "VersionUpdater" task could not be loaded from the assembly C:...\VersionUpdater.dll. Could not load file or assembly 'file:///C:...\VersionUpdater.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

好了,我估计可能是DLL必须使用的.Net核心现在建的,所以我做到了,并创造VersionUpdater.Core.dllAssemblyFile="VersionUpdater.Core.dll"),并得到了这个错误:

Severity Code Description Project File Line Suppression State Error MSB4062 The "VersionUpdater" task could not be loaded from the assembly C:...\VersionUpdater.Core.dll. Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

正如我所说的,代码工作正常.NET项目。它只是不想与.Net Core项目一起工作。我错过了什么?是.Net核心找不到System.Runtime的错误?

(来源就在这里:https://github.com/rjamesnw/VersionUpdater

为了使任务工作既“DOTNET的MSBuild”和MSBuild.exe,你需要交叉编译的.NET Framework和任务.NET核心 - 兼容的框架,如.NET标准。然后,您需要改变基于装配的任务和MSBuild的运行时类型。你可以使用MSBuildRuntimeType来检测这个。例如,

<PropertyGroup> 
    <TaskAssembly Condition=" '$(MSBuildRuntimeType)' == 'Core'">.\bin\Debug\netstandard1.6\MyTaskAssembly.dll</TaskAssembly> 
    <TaskAssembly Condition=" '$(MSBuildRuntimeType)' != 'Core'">.\bin\Debug\net46\MyTaskAssembly.dll</TaskAssembly> 
    </PropertyGroup> 

    <UsingTask TaskName="MyTaskName" AssemblyFile="$(TaskAssembly)" /> 

请参阅此博客文章的详细说明和示例。 http://www.natemcmaster.com/blog/2017/07/05/msbuild-task-in-nuget/