Main strategy:
Here we have a library NugLibEnforceCore, which contains a
custom MSBuild task and we will put it in a folder: The solution folder\.libs
First step is to build this library and put it in the proper folder.
Then we may try to build the `Consumer` project, which has build.targets and will try to run that custom tasks.
Projects and tasks are the bare minimum needed things for what expected and they don't have anything special.
The .targets file in the Consumer project:
<PropertyGroup> <LibFolder>$(SolutionDir).libs</LibFolder> <!-- <LibFolderPart2 Condition="'$(MSBuildRuntimeType)' == 'Core'">netstandard2.1\NugLibEnforceCore.dll</LibFolderPart2> --> <LibFolderPart2 Condition="'$(MSBuildRuntimeType)' == 'Core'">NugLibEnforceCore.dll</LibFolderPart2> <LibFolderPart2 Condition="'$(MSBuildRuntimeType)' != 'Core'">net472\NugLibEnforceCore.dll</LibFolderPart2> </PropertyGroup> <UsingTask TaskName="TestTask" AssemblyFile="$(LibFolder)\$(LibFolderPart2)" /> <Target Name="DoTheTask" AfterTargets="Build"> <Message Text="=== Trying TestTask ===" Importance="high" /> <TestTask /> <Message Text=" == TestTask is finished" Importance="high" /> </Target>
Here I share the issues and some difficulties I experienced and the approaches I tried related to the topic.
Tried to reproduce an issue which was irritating and happened time to time for me.
I was able to use custom tasks, but want to find the reasons and solve this issue forever.
1. Approach 1: ALL based on .net core - Build via Visual Studio Build Menu
get this error:
Error MSB4062 The "TestTask" task could not be loaded from the assembly ...\MSBuild13 Enforce Core\.libs\net472\WeRTheBest.NugLibEnforceCore.dll. Could not load file or assembly 'file:///...\MSBuild13 Enforce Core\.libs\net472\WeRTheBest.NugLibEnforceCore.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> 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. Consumer ...\MSBuild13 Enforce Core\Consumer\build\build.targets 27
---
**2. Approach 2:** ALL based on .net core - Build via dotnet msbuild command
dotnet msbuild NugLibEnforceCore\NugLibEnforceCore.csproj
get this error:
Error:
=== Trying TestTask === ...\MSBuild13 Enforce Core\Consumer\build\build.targets(27,5): error MSB4062: The "TestTask" task could not be loaded from the assembly ...\MSBuild13 Enforce Core\Consumer\build\.libs\netstandard2.1\NugLibEnforceCore.dll. Could not load file or assembly '...\MSBuild13 Enforce Core\Consumer\build\.libs\netstandard2.1\NugLibEnforceCore.dll'. The system cannot find the path specified. Confirm that the <UsingTask> 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. ...\MSBuild13 Enforce Core\Consumer\Consumer.csproj]
3. Approach 3: Multi-target (.Net Core & .Net Framework) - Build via Visual Studio Build Menu:
will cause other difficulties, which I may bring more details on the next steps here if needed or as we continue this discussion.
The code is also provided here:
https://github.com/KKacer/MSBuild13-Enforce-Core
Brief:
In the first approach when the project is based on ".net standard" why `MSBuildRuntimeType` is trying to take net472 library?!
In the second approach, it can't understand the same path used in the first approach and is looking for:
...\[Solution]\Consumer\build\.libs\netstandard2.1\NugLibEnforceCore.dll
instead of
...\[Solution]\.libs\netstandard2.1\NugLibEnforceCore.dll
In the last build I commented/removed "netstandard2.1" for easier builds, cause it is currently a single-target assembly.
We know that the files are in the proper location, you can also put the library manually and take a test if needed, the only thing that matters is be able to run the consumer project's tasks without any issues and worry.
I can share videos about it if needed.