如何在 C#8.0 中使用 BuildManager?

问题描述 投票:0回答:1

我有一个

.csproj
配置如下:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

当我使用以下 msbuild 命令时,我可以构建项目。

msbuild MyProject.csproj /t:Restore;Rebuild;Publish /p:PublishSingleFile=True /p:SelfContained=True /p:PublishProtocol=FileSystem /p:Configuration=Release /p:PublishDir=bin\Release\Test /p:RuntimeIdentifier=win-x86 /p:PublishReadyToRun=False /p:PublishTrimmed=False

但是我想使用 BuildManager 来代替。

我尝试过以下代码:

 var properties = new Dictionary<string, string>
 {
     ["Configuration"] = "Release",
     ["Platform"] = "x86",
     ["RuntimeIdentifier"] = "win-x86",
     ["SelfContained"] = "True",
     ["PublishSingleFile"] = "True",
     ["PublishReadyToRun"] = "False",
     ["PublishTrimmed"] = "False",
     ["PublishProtocol"] = "FileSystem",
     ["PublishDir"] = @"bin\Release\Test"
 };

 var targetsToBuild = new[]
 {
     "Restore",
     "Rebuild",
     "Publish"
 };

 var request = new BuildRequestData(path, properties, null, targetsToBuild, null);
 var parameters = new BuildParameters(new ProjectCollection());
 BuildManager.DefaultBuildManager.ResetCaches();
 var result = BuildManager.DefaultBuildManager.Build(parameters, request);

但是,我遇到以下异常:

The SDK 'Microsoft.NET.Sdk' specified could not be found.

为什么 BuildManager 的行为与 msbuild 不同,有没有办法解决这个问题?

msbuild c#-8.0 buildmanager
1个回答
0
投票

安装后您是否尝试验证您的网络 SDK 并检查了 envo?

© www.soinside.com 2019 - 2024. All rights reserved.