通过 Nuget 使用 .NET Standard 和 VS 2017 分发自定义 MS 构建任务 [英] Distribute custom MS Build Task with .NET Standard and VS 2017 via Nuget

查看:28
本文介绍了通过 Nuget 使用 .NET Standard 和 VS 2017 分发自定义 MS 构建任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Standard 库 (1.4) VS 2017 项目,其中包含需要通过 Nuget 包分发的自定义 MS Build 任务 (MyTask)(假设 MyCustomTask.dll 并且它包含 MyTask 和 Portable.targets由目标项目导入)

I have a .NET Standard library (1.4) VS 2017 project that contains custom MS Build task (MyTask) that need to be distributed via Nuget package (Let's say MyCustomTask.dll and it contains MyTask and Portable.targets that will be imported by target project)

这个带有自定义构建任务的 Nuget 包然后被目标 .NET Standard (1.4) 项目 cspro 文件用于导入调用自定义构建任务的 Portable.targets.

This Nuget package with custom build task is then used by target .NET Standard (1.4) project cspro file to import the Portable.targets that invoke the Custom Build task.

然而,此时我不断收到构建错误无法加载文件或程序集 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一.

However, at this point I keep on getting the build error Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

我尝试过 .NET Standard(1.4、1.5 和 1.6)但同样的错误.

I tried .NET Standard (1.4, 1.5 and 1.6) but same error.

推荐答案

问题在于,在这种情况下,消费应用程序 MSBuild.exe 需要包含运行 netstandard 任务所需的所有转发程序集(例如,依赖于NETStandard.Library).

The problem is that the consuming application, MSBuild.exe in this case, would need to include all the forwarding assemblies necessary to run netstandard tasks (e.g. depend on the NETStandard.Library).

在这种情况下,最好的解决方案是将任务库多定位到 .net 框架和 .net 标准目标框架:

The best solution in this case is multi-targeting the task library to a .net framework and a .net standard target framework:

<TargetFrameworks>netstandard1.6;net46</TargetFrameworks>

这个想法是有 2 个包含任务的 dll.在NuGet包中包含的项目文件中,而不是在中直接使用dll路径,思路是基于$(MSBuildRuntimeType)使用不同的dll文件code> 属性,在 MSBuild 的 .NET Core 版本上将是 Core:

The idea is to have 2 dlls that will contain the task. In the project files contained in the NuGet package instead of using a dll path directly in <UsingTask>, the idea is to using a different dll file based on the $(MSBuildRuntimeType) property, which will be Core on the .NET Core version of MSBuild:

<PropertyGroup>
  <_CustomTaskAssemblyTFM Condition="'$(MSBuildRuntimeType)' == 'Core'">netstandard1.6</_CustomTaskAssemblyTFM>
  <_CustomTaskAssemblyTFM Condition="'$(MSBuildRuntimeType)' != 'Core'">net46</_CustomTaskAssemblyTFM>
  <_CustomTaskAssembly>$(MSBuildThisFileDirectory)..	ools$(_CustomTaskAssemblyTFM)CustomTaskAssemblyName.dll</_CustomTaskAssembly>
</PropertyGroup>

<UsingTask TaskName="SomeCustomTask" AssemblyFile="$(_CustomTaskAssembly)" />

您可以在 asp 中看到这方面的示例.net 核心构建工具.NET Core SDK.

这篇关于通过 Nuget 使用 .NET Standard 和 VS 2017 分发自定义 MS 构建任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆