Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jhon65496 opened this issue May 16, 2024 · 1 comment
Assignees
Labels

Comments

@jhon65496
Copy link

jhon65496 commented May 16, 2024

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;

@JaynieBai
Copy link
Member

Repro this issue with the following code. It uses the MSBuildLocator to locate the MSBuild assemblies. But there is no MSBuild assembly located.

Image

using Microsoft.Build.Construction;
using Microsoft.Build.Locator;

MSBuildLocator.RegisterDefaults();
var solution = SolutionFile.Parse(@"");

project file

<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.9.5" ExcludeAssets="runtime" />
    <PackageReference Include="Microsoft.Build.Locator" Version="1.7.8" />
  </ItemGroup>

</Project>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants