通过Nuget分发带有.NET Standard和VS 2017的自定义MS Build Task [英] Distribute custom MS Build Task with .NET Standard and VS 2017 via Nuget

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

问题描述

我有一个.NET标准库(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)

目标.NET Standard(1.4)项目cspro文件随后使用带有自定义构建任务的Nuget程序包导入调用自定义构建"任务的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,版本= 4.1.0.0,文化=中性,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软件包中包含的项目文件中,而不是直接在<UsingTask>中使用dll路径,其想法是基于$(MSBuildRuntimeType)属性使用不同的dll文件,该属性在.NET Core版本上将为Core MSBuild:

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)..\tools\$(_CustomTaskAssemblyTFM)\CustomTaskAssemblyName.dll</_CustomTaskAssembly>
</PropertyGroup>

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

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

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

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