InvalidProjectFileException with using Microsoft.NET.Sdk in Microsoft.Build.Evaluation

Viewed 107

I want to parse a C#-project to read out all project- and other references. For months, I used the Microsoft.Build.Evaluation namespace from the package Microsoft.Build.

var file = Directory.GetFiles(rootDirectory, "*.csproj").First();
var project = new Project(test);

For the old csproj-files it is working fine without any problem. The old files are starting with

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Since migrating to the new SDK-Project style, I have some problems with reading the csproj files. They start with

<Project Sdk="Microsoft.NET.Sdk">

When loading the project I get the following exception: InvalidProjectFileException: 'The SDK 'Microsoft.NET.Sdk' specified could not be found. I figured out, if I change the Version of the package Microsoft.Build to the version of my Visual Studio/MSBuild (e.g. 16.8) it works fine. I have a lot of colleagues with a high variety of MSBuild Versions >15 and we cannot have all the same MSBuild version.

A possible solution which I have found is to add the Microsoft.Build.Runtime package but if I add it to my project I get a lot of compiler errors and no type is found which works before.

The project which should load the csproj files is a ToolsVersion="4.0"-project style with .NET Framework 4.7.2 (cannot change this to SDK project style because of massive T4 template usage).

0 Answers
Related