Skip to content

[Bug]: Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0'. The specified file cannot be found #10142

Closed
@jhon65496

Description

@jhon65496

Issue Description

The full text of the error

System.IO.FileNotFoundException
  HResult=0x80070002
  Сообщение = Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The specified file cannot be found..
  Источник = CodeAnalysisProjectsAnws03.My.NET70

Steps to Reproduce

Project https://github.com/jhon65496/CodeAnalysisProjects.NET7


Steps to Reproduce:

  • I'm running debugging in Visual Studio.
  • The error is located in the line Test1();.

The error is located
The error is located in the line Test1();.

Program.cs

internal class Program
{

    static void Main(string[] args)
    {
        Test1();
    }

    
    static void Test1()
    {
        string path = @"e:\Projects\WPF\GenericCodes.WPF.sln";

        List<Project> table = ProjectAnalysisService.GetProjects(path);            
    }
}

ProjectAnalysisService.cs

using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.MSBuild;

public static class ProjectAnalysisService
{

    public static List<Project> GetProjects(string solutionPath)
    {
        // Используйте MSBuildLocator для регистрации версии MSBuild
        MSBuildLocator.RegisterDefaults();
        // using var collection = new ProjectCollection();
        using var workspace = MSBuildWorkspace.Create();
        var solution = SolutionFile.Parse(solutionPath);

        var solutionProjects = solution.ProjectsInOrder
                                    .Select(project => workspace.OpenProjectAsync(project.AbsolutePath).Result)
                                    .Cast<Microsoft.Build.Evaluation.Project>() // Добавьте эту строку
                                    .ToList();
                    
        return solutionProjects;
    }
}

CodeAnalysisProjects.NET7.csproj

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.Build" Version="17.4.0" ExcludeAssets="runtime" />
		<PackageReference Include="Microsoft.Build.Framework" Version="17.9.5" ExcludeAssets="runtime" />
		<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
		<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.10.0" />
	</ItemGroup>
</Project>

Expected Behavior

Working without error

Actual Behavior

Working with an error

Analysis

dfgdfgdfg

Versions & Configurations

I use:
- Win10x64;
- NET-7;
- Console;
- Microsoft Visual Studio Community 2022 (64-bit version) - Current
Version 17.5.3;

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions