Benjamin Rothman
Benjamin Rothman

Reputation: 41

Custom MSBuild task .NET 6.0 runs with dotnet but not in VS

I have a simple project that has a custom MSBuild task. This task will run using dotnet msbuild, but fails through VS2022. I haven't found anything online other than some issues around architecture introduced with .NET 6.0 which I don't believe would apply to this. I think given that the dotnet cli will build this without issue, I must be missing something, only question is what?

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.2.0" />
    </ItemGroup>
    <UsingTask TaskName="PublishValidationTask" 
               AssemblyFile="..\bin\Debug\net6.0\ConsoleApp25.dll" />
    <Target Name="MyTarget" 
            AfterTargets="Build">
        <PublishValidationTask />
    </Target>
</Project>
using Microsoft.Build.Framework;
using Task = Microsoft.Build.Utilities.Task;

namespace ConsoleApp25;

public class HelloWorldTask : Task
{
    public override bool Execute()
    {
        Log.LogMessage(MessageImportance.High, "Hello World");            
        return true;
    }
}
Severity    Code    Description Project File    Line    Suppression State
Error   MSB4018 The "HelloWorldTask" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at ConsoleApp25.HelloWorldTask.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Upvotes: 4

Views: 3119

Answers (1)

Omer Ran
Omer Ran

Reputation: 31

Microsoft documentation explains that because Visual Studio is running .Net Framework, tasks running from within Visual Studio should target .NET Standard 2.0, see https://learn.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-code-generation?view=vs-2022

At the worst case, you could build a console application and run it with the built-in Exec task.

Upvotes: 3

Related Questions