Roslyn – CodeAnalysis.MsBuild – How to open project?

I’m trying to generate a file containing metadata about the analyzed code, given a csproj. I’m failing at step #1, opening the project.

public static async Task Main(string[] args) {     MSBuildLocator.RegisterDefaults();     using (var workspace = MSBuildWorkspace.Create())     {         workspace.WorkspaceFailed += (_, failure) => Console.WriteLine(failure.Diagnostic);          var project = await workspace.OpenProjectAsync(args[0]);     } } 

Analyzer project file:

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>     <TargetFramework>netcoreapp2.1</TargetFramework>     <ApplicationIcon />     <OutputType>Exe</OutputType>     <StartupObject />     <LangVersion>latest</LangVersion>   </PropertyGroup>    <ItemGroup>     <PackageReference Include="Markdig" Version="0.20.0" />     <PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />     <PackageReference Include="Microsoft.CodeAnalysis" Version="3.4.0" />     <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.4.0" />     <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />   </ItemGroup> </Project> 

This outputs

[Failure] Msbuild failed when processing the file 'C:\projects\NoComment\NoComment.SimplestAssembly\NoComment.SimplestAssembly.csproj' with message: C:\Program Files\dotnet\sdk\3.1.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets: (234, 5): The "ResolvePackageAssets" task failed unexpectedly. System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.ProjectModel, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. File name: 'NuGet.ProjectModel, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35' ---> System.IO.FileNotFoundException: Could not load the specified file. File name: 'NuGet.ProjectModel'    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName)    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader.CreateReaderFromMemory(ResolvePackageAssets task, Byte[] settingsHash)    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader..ctor(ResolvePackageAssets task)    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ReadItemGroups()    at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ExecuteCore()    at Microsoft.NET.Build.Tasks.TaskBase.Execute()    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)   [Failure] Msbuild failed when processing the file 'C:\projects\NoComment\NoComment.SimplestAssembly\NoComment.SimplestAssembly.csproj' with message: C:\Program Files\dotnet\sdk\3.1.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.GenerateAssemblyInfo.targets: (163, 5): The "GetAssemblyVersion" task failed unexpectedly. System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.Versioning, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. File name: 'NuGet.Versioning, Version=5.6.0.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35' ---> System.IO.FileNotFoundException: Could not load the specified file. File name: 'NuGet.Versioning'    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(AssemblyName assemblyName)    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)    at Microsoft.NET.Build.Tasks.GetAssemblyVersion.ExecuteCore()    at Microsoft.NET.Build.Tasks.TaskBase.Execute()    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) 

The project I’m trying to open is really simple, the default class library template:

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>     <TargetFramework>netcoreapp2.1</TargetFramework>   </PropertyGroup>  </Project> 
namespace SimplestAssembly {     public class Class1     {     } } 
Add Comment
2 Answer(s)

Can you please add the following NuGet package references to your project and try again?

  • NuGet.Frameworks
  • NuGet.Packaging
  • NuGet.ProjectModel
  • NuGet.Versioning

MsBuild is trying to do a restore (ResolvePackageAssets task) and it needs these packages.

Add Comment

Just want to know more information about your issue. where you able to open the project before or this is the first time you are trying to creat and open the solution?

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.