Distribute custom MS Build task with .NET Standard and VS 2017 via Nuget

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

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

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

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

+3


source to share


1 answer


The problem is that in this case, the consuming MSBuild.exe application will need to include all the forwarding assemblies required to perform netstandard tasks (for example, depend on NETStandard.Library).

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

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

      



The idea is to have 2 dlls that will contain the task. In the project files contained in the NuGet package, instead of using the DLL path directly in <UsingTask>

, the idea is to use a different property based dll $(MSBuildRuntimeType)

that will be Core

in 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)" />

      

You can see examples of this in the asp.net core build tools and .NET Core SDK .

+4


source







All Articles