簡體   English   中英

自定義 MSBuild 任務 .NET 6.0 使用 dotnet 運行,但不在 VS 中運行

[英]Custom MSBuild Task .NET 6.0 Runs With dotnet but not in VS

我有一個簡單的項目,它有一個自定義 MSBuild 任務。 此任務將使用 dotnet msbuild 運行,但通過 VS2022 失敗。 除了與 .NET 6.0 引入的架構有關的一些問題外,我沒有在網上找到任何東西,我認為這些問題不適用於此。 我認為鑒於 dotnet cli 將毫無問題地構建它,我一定會遺漏一些東西,唯一的問題是什么?

<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].

Microsoft 文檔解釋說,由於 Visual Studio 正在運行.Net Framework,因此從 Visual Studio 中運行的任務應針對 .NET Standard 2.0,請參閱https://docs.microsoft.com/en-us/visualstudio/msbuild/tutorial-custom-task-代碼生成?view=vs-2022

在最壞的情況下,您可以構建一個控制台應用程序並使用內置的 Exec 任務運行它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM