How to use BuildManager with C#8.0?

25 Views Asked by At

I have a .csproj configured as follows:

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

When I use the following msbuild command, I am able to build the project.

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

I would however like to use BuildManager instead.

I have tried the following code:

 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);

However, I get the following exception:

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

Why is BuildManager behaving differently from msbuild, and is there a way I can fix this issue?

1

There are 1 best solutions below

0
On

Did u try to verify ur net sdk after installation and also checked envo?