2017-03-17 68 views
2

我有一個相當簡單的類,它繼承自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

回答

1

爲了使任務工作既「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/

相關問題