VS 2017 .NetCore MSBuild custom task fails to load Newtonsoft.Json or other dependents - msbuild

Environment
Visual Studio 2017
.NET Standard 2.0
.NET Framework 4.6.1
MyCustomTask.csproj contains custom MS build tasks and has multi targets to generate a separate dll for .NET Full and .NEt Core runtime.
There is a 3 level dependency on the Nuget Package "Microsoft.Extensions.Configuration.Json" which in turn has a dependency to Newtonsoft.Json package. as shown below.
The build is all fine and the Nuget package for MyCustomTask is generated successfully.
When I install my MyCustomTask Nuget package on target project then the custom build task is getting successfully executed on .NET Full runtime from Visual Studio 2017, but for .NET Core dotnet build command fail to execute the custom build task...
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral.
Even if I package all the dependent DLL and place it in same directory of the .netstandard custom task dll I keep getting same error.
Any thoughts how this can be resolved ?

Posting a solution here for those who are experiencing the same issue.
Credit to #MartinUllrich for posting the solution in a comment above. This saved me a lot of trouble.
https://github.com/AArnott/Nerdbank.MSBuildExtension
Ensure your custom task inherits from ContextIsolatedTask and copy the dependent assemblies to same directory as your task assembly.

Related

ASP.NET Core 2.2 - How to retreive NuGet Package Dependencies from Assembly that a Project References

I have a project ("Project1") that has a reference to an Assembly that I created in another project ("Project2").
I just added a function to a class in that assembly, but that function required me to add the NuGet package System.Drawing.Common. Which isn't an issue.
Here's the issue:
When I build Project1, it does not bring System.Drawing.Common.DLL to the bin folder. So, at runtime, when I execute this new function, I get an error stating that it can't find this DLL.
Is there a way that I can have Project1 bring along all of the Assembly's dependencies? Or do I have to instead add all the same NuGet packages that Project2/Assembly uses to Project1?
I'd prefer the former. I'm using ASP.NET Core 2.2 and Visual Studio 2017.
Thank you.
The build command with .NET Core 2.2 does not bring in the dependencies by default this is for testing only. If you are wanting all the dependency .dll to be included for deployment you should use the publish command to get all the .dll from your NuGet packages. There are other switches to include the framework as well so you don't need to install the framework, on the host.
As a friendly reminder, Dot Net Core 2.2 support will end on December 23, 2019

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

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)
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.
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.
I tried .NET Standard (1.4, 1.5 and 1.6) but same error.
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).
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>
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)" />
You can see examples of this in the asp.net core build tools and the .NET Core SDK.

dotnet publish failing using .NET Core 1.1 and new VS 2017 project format

We've recently migrated to Visual Studio 2017 Update 2. We have a .NET Core 1.1 app which targets .NET Framework 4.6.1 only, and has project references to .NET Framework class libraries in the same solution. We are using the 1.0.4 SDK version of dotnet.
One of the project references contains T4 templates which are configured to run on build. This requires importing Microsoft.TextTemplating.targets and related assemblies, so that the T4 templates can be transformed by MSBuild. These assemblies in turn require .NET Framework 4.6 assemblies (System, System.Data, System.Xml, etc).
Visual Studio 2017 has no problems building the code, however when we try to use dotnet build or dotnet publish in our TeamCity builds, the command fails with the following output:
c:\xxx\Microsoft.TextTemplating.targets(340,5): error MSB4018: The "TransformTemplates" task failed unexpectedly.
c:\xxx\Microsoft.TextTemplating.targets(340,5): error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
c:\xxx\Microsoft.TextTemplating.targets(340,5): error MSB4018: File name: 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Based on the research I've done so far, the .NET Framework references required to run the T4 transformation task are not being loaded by the dotnet CLI, but they are by Visual Studio, due to entirely separate MSBuild toolchains.
Is there any way to make dotnet publish work in the same manner as Visual Studio? This issue has completely broken our CI / CD trains.
NOTE: I tried including .NET Standard as a target framework to see if that might work, but we use several packages that are not compatible so that isn't an option (Entity Framework 6.1, for example).
The dotnet based commands run on a .NET Core build of MSBuild so it is unable to load full framework assemblies. If there are no .NET Core or .NET Standard versions of these assemblies, loading them during builds of the .NET Core version of MSBuild is impossible at the moment (this may become possible with the move to .NET Core 2.0).
In order to get CI/CD working, you need to use the VS version of MSBuild. dotnet publish only forwards to msbuild so you can get the same behaviour passing parameters to msbuild. For example:
dotnet publish -c Release
becomes
msbuild /m /t:Publish /p:Configuration=Release

Visual Studio 2017 MSBuild Task Development

With developing an custom MSBuild Task with Visual Studio 2017 RC, I have the following problem: As soon as I add other dependencies than just Microsoft.Build.Utilities.Core (using v15.1.0-preview-000458-02 for .NET Core Support), I cannot load the task into another .csproj MSBuild project as the dependencies are not found.
Is there a way to automatically copy all dependencies to the Debug folder?
Or do I have to publish it every time I want to test it?
Update1:
The problem with publish was something local to my environment and has been fixed.
Update2:
It seems that as soon as I change the TargetFramework from netstandard1.4 to netstandard1.6 it isn't even able to load the task at all. As soon as I use netstandard 1.6 it throws a an exception:
The task could not be loaded from the assembly.
Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its
dependencies.
Is there a way to automatically copy all dependencies to the Debug folder? Or do
I have to publish it every time I want to test it?
By default and for good reasons, .NET Core and .NET Standard projects do not copy referenced assemblies into the build folder. Instead, they are resolved them from the NuGet cache.
But if you really need it, this behavior can be changed by overriding the default with the CopyLocalLockFileAssemblies setting.
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
Cref: https://github.com/dotnet/sdk/blob/d20405f91a2959fa91fea6285d9a896286727f2a/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.BeforeCommon.targets#L55-L56
Second question
It seems that as soon as I change the TargetFramework from netstandard1.4 to netstandard1.6
To build a task assembly that works on both "MSBuild.exe" and "dotnet.exe msbuild", you should target netstandard1.4 or lower. netstandard1.6 is not compatible with .NET Framework 4.6.1 (which MSBuild.exe runs on.)
If you need API not available in netstandard1.4, you will need to cross-compile your task for .NET Framework and .NET Standard, which is considerably more complex but can be done.

Why can't MSBuild ToolsVersion 4.0 build older projects?

We're using Cruse Control to manage our build process.
AS we convert vs2008 projects to vs2010, we're leaving the target framework set at 3.5 for web and class library projects.
At this point we're not going through and converting all our solutions to vs2010; not if we don't have to.
I recently updated the MSbuild project files that cruise control uses to point at MSBuild 4.0 so our build process would be able to build vs2010 projects.
C:\windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
All was well until a web project that was targeting the 4.0 framework was committed.
At which point this error popped up:
CS0433: The type 'System.Web.Routing.RouteCollection' exists in both c:\Windows\Microsoft.NET\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll and c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Web.Routing.dll in Global.asax.cs(15, 43)
At which point I became aware of the Windows/Microsoft.NET/Assembly folders.
Now... All the MSBuild projects, even though they were using MSBuild 4.0, still had the tools version set at 3.5.
So why was a 3.5 targeted build looking at the new 4.0 assembly folders and finding this conflict? Probably because I was using MSBuild 4.0. But if I can change the toolVersion that MSBuild uses, you'd think I can tell it to target 3.5 without worrying about these potential conflicts.
To resolve this issue, I went to the relevant MSBuild project files that cruise control uses and changed their toolsVersion to 4.0. This got passed that conflict error. But now everytime it tries to build a project that's in a solution that we haven't yet converted to a vs2010 project, it breaks with an error like this:
MyProject.csproj in SomeFilePath:
LC0000: 'Could not load file or assembly or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.' in LC(0, 0)
If I open the solution that contains that project in Visual Studio 2010 and do the conversion, build it and commit that and force another build, I get passed that error only to find that another not yet converted project is tossing that same error.
So now I am for sure using MSBuild 4.0 and for real targeting the 4.0 framework. Why can't 4.0 build 3.5 projects or vs2008 solution projects?
The issue is that you have migrated part of your projects to to visual studio 2010. Any 2008 project that references a 2010 project (by project reference) will give you this compile time error. Try updating all .csproj files to 2010 and try rebuilding again.