Roslyn,MSBuildWorkspace编译引用.NetStandard项目的.NetStandard项目会抛出“找不到RuntimeMetadataVersion值". [英] Roslyn, MSBuildWorkspace compiling a .NetStandard project referencing a .NetStandard project throws "No value for RuntimeMetadataVersion found."
问题描述
使用以下项目创建解决方案.
Create a solution with the following projects.
一个.NetStandard2.0项目,将其命名为"1"
A .NetStandard2.0 project, name it "1"
一个.NetStandard2.0项目,将其命名为"2"
A .NetStandard2.0 project, name it "2"
在2中添加项目1的引用
Add a reference of project 1 in 2
一个.Net4.6.1控制台项目将其命名为"3",并添加nuget Microsoft.CodeAnalysis.CSharp.Workspaces v2.7.0
A .Net4.6.1 console project name it "3" and add the nuget Microsoft.CodeAnalysis.CSharp.Workspaces v2.7.0
将以下代码粘贴到项目3中
Paste in the follwing code into project 3
static void Main(string[] args)
{
string dir = Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\");
string solutionPath = Directory.GetFiles(dir, "*.sln").First();
var msWorkspace = MSBuildWorkspace.Create();
Solution solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;
DateTime date = DateTime.UtcNow;
foreach (var project in solution.Projects.Take(2))
{
Compilation compilation = project.GetCompilationAsync().Result;
using (var dllStream = new MemoryStream())
{
var emitResult = compilation.Emit(dllStream);
if (emitResult.Success)
{
Console.WriteLine("Success");
}
else
{
foreach (var err in emitResult.Diagnostics)
{
Console.WriteLine($"{err.ToString()}");
}
}
}
}
Console.Read();
}
执行项目3会为项目2引发以下错误,但不会为项目1引发以下错误.
executing project 3 throws the following errors for project 2, but not for project 1.
警告CS8021:未找到RuntimeMetadataVersion的值.没有找到包含System.Object的程序集,也没有通过选项指定RuntimeMetadataVersion的值.
warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
d:\ test \ roslyntest \ 2 \ Class1.cs(1,7):错误CS0246:找不到类型或名称空间名称'System'(您是否缺少using指令或程序集引用?)
d:\test\roslyntest\2\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?)
d:\ test \ roslyntest \ 2 \ Class1.cs(5,18):错误CS0518:未定义或导入'System.Object'预定义类型
d:\test\roslyntest\2\Class1.cs(5,18): error CS0518: Predefined type 'System.Object' is not defined or imported
d:\ test \ roslyntest \ 2 \ Class1.cs(5,18):错误CS1729:'对象'不包含带有0个参数的构造函数
d:\test\roslyntest\2\Class1.cs(5,18): error CS1729: 'object' does not contain a constructor that takes 0 arguments
我的问题.为什么在项目1中找不到基本类,而在项目1中找不到基类?
My question. Why is it not finding the basic classes in project 2 but in project 1?
唯一的区别是项目2引用了另一个项目.
The only difference is that project 2 references another project.
我想念什么/做错了什么?
What am i missing/doing wrong?
推荐答案
要解决此问题,您需要执行以下步骤:
To address this, here are the steps you need to take:
- 参考
Microsoft.Build.Locator
NuGet包. - 在创建
MSBuildWorkspace
之前,调用 - 确保您的应用程序bin文件夹中没有任何Microsoft.Build.*程序集(请参见下文).
MSBuildLocator.RegisterDefaults();
通过这些步骤,我能够正确地构建2个项目.
With those steps I was able to correctly build the 2 projects.
这是从输出文件夹中删除文件的方法(将其添加到.csproj或使用Visual Studio中的生成后"步骤).
This is how you can remove the files from the output folder (add this to your .csproj or use the Post Build steps in Visual Studio).
<PropertyGroup>
<PostBuildEvent>del "$(TargetDir)\Microsoft.Build.dll"
del "$(TargetDir)\Microsoft.Build.Framework.dll"
del "$(TargetDir)\Microsoft.Build.Tasks.Core.dll"
del "$(TargetDir)\Microsoft.Build.Utilities.Core.dll"
</PostBuildEvent>
</PropertyGroup>
有关其他信息,请参见: https://github.com/dotnet/roslyn/issues/26029
For additional information please see: https://github.com/dotnet/roslyn/issues/26029
这篇关于Roslyn,MSBuildWorkspace编译引用.NetStandard项目的.NetStandard项目会抛出“找不到RuntimeMetadataVersion值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!