Trying to get a simple example to work using Microsoft.Build, but can't #10256
-
I'm trying to use the using Microsoft.Build.Locator;
using Microsoft.Build.Evaluation;
string projectFilePath = @"Path/To/ThirdPartyApp.csproj";
MSBuildLocator.RegisterDefaults();
// Load an existing project
Project proj = new Project(projectFilePath);
// Access an ItemGroup
ICollection<ProjectItem> itemGroup = proj.GetItems("Compile");
// Print all files matched by the Item
foreach (ProjectItem item in itemGroup)
{
Console.WriteLine(item.EvaluatedInclude);
} The project file for my application (i.e. the one that's supposed to do the probing, not <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="17.10.4" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.8" />
</ItemGroup>
</Project> When I ran it, however I got the following error:
I then tried to add the
When I run the app, however I get a runtime error:
Is there something fundamentally wrong with how I'm trying to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Can you see if the docs answer your question, specifically this part? https://learn.microsoft.com/visualstudio/msbuild/find-and-use-msbuild-versions?view=vs-2022#register-instance-before-calling-msbuild Unfortunately we can't do much with the error message for this problem, since it's thrown by the .NET runtime itself. |
Beta Was this translation helpful? Give feedback.
@amine-aboufirass, note this text in the documentation:
In your code, the
Project proj = new Project(projectFilePath);
statement seems to be in the same method as theMSBuildLocator.RegisterDefaults()
call. You should move it to a second method, so that things can proceed in this order:Project
, the .NET Runtime does not need to find the definitions of those types yet.