注意,我也曾问过这个问题(带有复制代码)on Roslyn's GitHub
请注意,我已经尝试将net461,netcoreapp2.1和netstandard2.0作为项目的目标框架-每次都遇到相同的问题。
static async Task Main(string[] args)
{
MSBuildLocator.RegisterDefaults();
using (var workspace = MSBuildWorkspace.Create())
{
workspace.WorkspaceFailed += (sender, workspaceFailedArgs) => WriteLine(workspaceFailedArgs.Diagnostic.Message);
var solution = await workspace.OpenSolutionAsync(@"c:\source\ForRoslynTest\ForRoslynTest.sln");
WriteLine($"Loaded solution {solution.FilePath}");
var projectTree = workspace.CurrentSolution.GetProjectDependencyGraph();
foreach (var projectId in projectTree.GetTopologicallySortedProjects())
{
await CompileProject(workspace.CurrentSolution.GetProject(projectId));
}
}
}
private static async Task CompileProject(Project project)
{
WriteLine($"Compiling {project.Name}. It has {project.MetadataReferences.Count} metadata references.");
var compilation = await project.GetCompilationAsync();
var errors = compilation.GetDiagnostics().Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error);
if (errors.Any())
{
WriteLine($"COMPILATION ERROR: {compilation.AssemblyName}: {errors.Count()} compilation errors: \n\t{string.Join("\n\t", errors.Where(e => false).Select(e => e.ToString()))}");
}
else
{
WriteLine($"Project {project.Name} compiled with no errors");
}
}
您将收到以下输出:
Msbuild failed when processing the file 'c:\source\ForRoslynTest\DownstreamLibrary\DownstreamLibrary.csproj' with message: C:\Program Files\dotnet\sdk\2.1.602\Microsoft.Common.CurrentVersion.targets: (1548, 5): The "Microsoft.Build.Tasks.ResolveNonMSBuildProjectOutput" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
Found project reference without a matching metadata reference: c:\source\ForRoslynTest\CoreLibrary\CoreLibrary.csproj
Loaded solution c:\source\ForRoslynTest\ForRoslynTest.sln
Compiling CoreLibrary. It has 113 metadata references.
Project CoreLibrary compiled with no errors
Compiling DownstreamLibrary. It has 0 metadata references.
COMPILATION ERROR: DownstreamLibrary: 3 compilation errors:
c:\source\ForRoslynTest\DownstreamLibrary\Class1.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
c:\source\ForRoslynTest\DownstreamLibrary\Class1.cs(5,18): error CS0518: Predefined type 'System.Object' is not defined or imported
c:\source\ForRoslynTest\DownstreamLibrary\Class1.cs(5,18): error CS1729: 'object' does not contain a constructor that takes 0 arguments
所以我的问题是如何解决以上错误并让DownstreamLibrary进行编译?
编辑我99%确信根本原因是此错误
无法从程序集Microsoft.Build.Tasks.Core,版本= 15.1.0.0中加载“ Microsoft.Build.Tasks.ResolveNonMSBuildProjectOutput”任务
我已用procmon确认它正在加载以下DLL(C:\ Program Files \ dotnet \ sdk \ 2.1.602 \ Microsoft.Build.Tasks.Core.dll),我已用ILSpy确认DOESN' T中有ResolveNonMSBuildProjectOutput任务。此DLL did的较旧版本具有此任务。
我找到了解决此异常的手动方法:
[[Failure] Msbuild在处理with消息时失败:无法从程序集中Microsoft.Build.Tasks.Core,版本= 15.1.0.0,Culture = neutral,PublicKeyToken加载“ Microsoft.Build.Tasks.ResolveNonMSBuildProjectOutput”任务= b03f5f7f11d50a3a。确认声明正确无误,并保证程序集及其所有依赖项都可用,并且任务包含实现Microsoft.Build.Framework.ITask的公共类。
有一些解决步骤:
1.0.0/ 1.0.0-rc4-004771/ 2.0.3/ 2.1.505/
1.0.0-preview1-002702/ 1.0.3/ 2.1.202/ 3.1.100/
1.0.0-preview2-003121/ 1.0.4/ 2.1.4/ 3.1.201/
1.0.0-preview4-004233/ 1.1.0/ 2.1.502/ NuGetFallbackFolder/
成功!该错误消失了,但是运行我的roslyn应用程序后导致了一些错误,例如(路径被剥离)
[[Failure] Msbuild在处理带有消息的文件时失败,“ ProcessFrameworkReferences”任务意外失败。 System.IO.FileNotFoundException:无法加载文件或程序集'NuGet.Frameworks,版本= 5.5.0.4,区域性=中性,PublicKeyToken = 31bf3856ad364e35'。该系统找不到指定的文件。文件名:'NuGet.Frameworks,版本= 5.5.0.4,文化=中性,PublicKeyToken = 31bf3856ad364e35'
通过在我的项目中添加NuGet.ProjectModel 5.5.1解决了这个问题,csproj现在具有以下程序包引用:
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="16.5.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.5.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="16.5.0" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.5.0" />
<PackageReference Include="NuGet.ProjectModel" Version="5.5.1" />
</ItemGroup>
此代码没有更多WorkspaceFailed事件:
Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults();
var workspace = MSBuildWorkspace.Create();
workspace.WorkspaceFailed += (s, e) => { Console.WriteLine(e.Diagnostic); };
var project = await workspace.OpenProjectAsync(@"C:/path/to.csproj");
我正在与Roslyn一起加载的csproj看起来如下:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<OutputType>Library</OutputType>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\dependencies\some.csproj" />
</ItemGroup>
</Project>