简体   繁体   English

在 NetCoreApp 中使用 Roslyn:未安装 MSBuildWorkspace

[英]Using Roslyn in NetCoreApp: MSBuildWorkspace not installed

I am trying to learn how to use Roslyn to extract info from c# source files.我正在尝试学习如何使用 Roslyn 从 c# 源文件中提取信息。 But I can't even get started, because I can't seem to find the right way to install the right packages.但是我什至无法开始,因为我似乎无法找到安装正确软件包的正确方法。

I'm trying to build a simple netcoreapp1.1 console app.我正在尝试构建一个简单的 netcoreapp1.1 控制台应用程序。 Its csproj file looks like this:它的 csproj 文件如下所示:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis" Version="2.0.0" />
    <PackageReference Include="Microsoft.Composition" Version="1.0.30" />
  </ItemGroup>

</Project>

I thought from what I'd read online that this would be sufficient to load whatever assembly contains MSBuildWorkspace...but apparently not.我认为根据我在网上阅读的内容,这足以加载任何包含 MSBuildWorkspace 的程序集……但显然不是。

How do I go about getting the project configured properly?我该如何正确配置项目?

Unfortunately, MSBuildWorkspace is currently only supported on .Net Framework .不幸的是, MSBuildWorkspace目前仅在 .Net Framework 上受支持 You won't be able to use it from .Net Core.您将无法从 .Net Core 使用它。

The accepted answer seems no longer up to date: there is a NuGet package Microsoft.CodeAnalysis.Workspaces.MSBuild that supports .NET Core Console Apps:接受的答案似乎不再是最新的:有一个支持 .NET Core 控制台应用程序的NuGet 包Microsoft.CodeAnalysis.Workspaces.MSBuild

在此处输入图片说明

Trying out that package with these lines:使用这些行尝试该包:

using Microsoft.CodeAnalysis.MSBuild;
...
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionFile.FullName).Result;

... it threw an exception. ...它抛出了一个异常。 I found a posting ( https://github.com/dotnet/roslyn/issues/17401#issuecomment-282605950 ) that added a few other NuGet packages that seem to be dependencies.我发现了一个帖子( https://github.com/dotnet/roslyn/issues/17401#issuecomment-282605950 ),其中添加了一些其他似乎是依赖项的 NuGet 包。 That made the references in my csproj:这使得我的 csproj 中的引用:

<PackageReference Include="Microsoft.Build" Version="16.7.0" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.7.0" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="16.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.7.0" />

and with these references I got it to work (I'm working on Windows).并通过这些参考我让它工作(我在 Windows 上工作)。

However, I ran into the issue that that Microsoft.CodeAnalysis.Project's Documents Count was 0 and someProject.GetCompilationAsync().Result.SyntaxTrees was null.但是,我遇到了 Microsoft.CodeAnalysis.Project 的文档计数为 0 并且 someProject.GetCompilationAsync().Result.SyntaxTrees 为空的问题。 The "MSBUILD_EXE_PATH" is not correctly set (see https://github.com/dotnet/roslyn/issues/22415 ). “MSBUILD_EXE_PATH”未正确设置(参见https://github.com/dotnet/roslyn/issues/22415 )。 At first I chose to set the most recent MSBuild.dll this way:一开始我选择这样设置最新的MSBuild.dll:

    private static void SetMSBuildPath()
    {
        var latestSdk = new DirectoryInfo(@"C:\Program Files\dotnet\sdk\").EnumerateDirectories().Where(f => Regex.IsMatch(f.Name, @"\d+\.\d+\.\d+"))
            .OrderByDescending(f => f.Name.Split(".")[0])
            .ThenByDescending(f => f.Name.Split(".")[1])
            .ThenByDescending(f => f.Name.Split(".")[2])
            .FirstOrDefault();

        Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", Path.Combine(latestSdk.FullName, "MSBuild.dll"));
    }

but a better way is to use the NuGet package Microsoft.Build.Locator and invoke it at the start of your application once like this:但更好的方法是使用 NuGet 包 Microsoft.Build.Locator 并在应用程序开始时调用它,如下所示:

MSBuildLocator.RegisterDefaults();

For this to work, make sure that you exclude MSBuild from the Microsoft.Build.* NuGet packages you use (except from Microsoft.Build.Locator ) as suggested by the documentation (this changes the PackageReferences shown above):为此,请确保按照文档的建议从您使用的Microsoft.Build.* NuGet 包( Microsoft.Build.Locator除外)中排除 MSBuild(这会更改上面显示的 PackageReferences):

<PackageReference Include="Microsoft.Build" Version="16.7.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.7.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="16.7.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.7.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.7.0" />

PS: I haven't looked into it, but maybe https://github.com/daveaglick/Buildalyzer (NuGet package "Buildalyzer") solves all of these problems. PS:我没有研究过,但也许https://github.com/daveaglick/Buildalyzer(NuGet包“Buildalyzer”)可以解决所有这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Roslyn MSBuildWorkspace找到C#项目的&#39;obj&#39;目录? - How to find the 'obj' directory for C# projects using Roslyn MSBuildWorkspace? Output 一个 Roslyn MSBuildWorkspace 到不同的文件夹 - Output a Roslyn MSBuildWorkspace to different folder 将Roslyn MSBuildWorkspace与Visual Studio 2013一起使用 - Use Roslyn MSBuildWorkspace with Visual Studio 2013 使用 Roslyn MSBuildWorkspace 项目 AddAnalyzerReference 不加载分析器 - Use Roslyn MSBuildWorkspace Project AddAnalyzerReference doesn't load analyzers Roslyn,MSBuildWorkspace.Create无法与msbuild 14或12一起使用 - Roslyn, MSBuildWorkspace.Create not working with msbuild 14 or 12 Roslyn / MSBuildWorkspace不会在非Dev计算机上加载文档 - Roslyn / MSBuildWorkspace Does Not Load Documents on Non-Dev Computer Roslyn MsBuildWorkspace编译发出的内容不包含我的更改 - Roslyn MsBuildWorkspace compilation emit does not contain my changes 编译引用.NetStandard项目的.NetStandard项目的Roslyn,MSBuildWorkspace抛出“找不到RuntimeMetadataVersion的值”。 - Roslyn, MSBuildWorkspace compiling a .NetStandard project referencing a .NetStandard project throws “No value for RuntimeMetadataVersion found.” 执行安装在全局nuget缓存中的NetCoreApp - Execute NetCoreApp installed in global nuget cache Roslyn脚本 - 使用别名 - Roslyn scripting - using aliases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM