通过Roslyn编译时找不到“主要”方法 [英] 'Main' method not found when compiling through Roslyn
问题描述
我正在使用Roslyn使用运行时生成的代码来编译解决方案。虽然从Visual Studio打开该解决方案时可以完美编译,但从Roslyn失败:
I am using Roslyn to compile a solution with run-time generated code. While the solution compiles perfectly when opened from Visual Studio, it fails from Roslyn:
错误CS5001:程序不包含静态 Main适用于入口点的方法
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
我要编译的解决方案只有一个ASP.NET Core 2(。 NET Framework 4.6.2)项目,该项目当然在项目的根目录下的Program类中具有Main方法:
The solution I am trying to compile has a single ASP.NET Core 2 (.NET Framework 4.6.2) project, which of course has a Main method in the Program class at the root of the project:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
这是我正在编译的代码解决方案,来自.NET 4.7 WPF应用程序:
This is the code I'm running to compile that solution, from a .NET 4.7 WPF application:
private static async Task<bool> CompileSolution(string solutionPath, string outputDir)
{
var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync(solutionPath);
var projectCompilation = await solution.Projects.Single().GetCompilationAsync();
if (string.IsNullOrEmpty(projectCompilation?.AssemblyName))
{
return false;
}
using (var stream = File.Create(Path.Combine(outputDir, $"{projectCompilation.AssemblyName}.dll")))
{
var result = projectCompilation.Emit(stream);
return result.Success;
}
}
projectCompilation.Emit
失败,并显示以下信息:
projectCompilation.Emit
fails with:
- 警告CS8021:未找到RuntimeMetadataVersion的值。没有找到包含System.Object的程序集,也没有通过选项指定
RuntimeMetadataVersion的值。
- 错误CS5001:程序不包含适用于$ b $的静态 Main方法b作为入口点
是否可能是所使用的NuGet软件包未正确支持。 NET Core 2项目了吗?我没有任何待处理(甚至没有预览)的程序包更新。
Could it be that the NuGet package being used does not correctly support .NET Core 2 projects yet? I do not have any pending (not even preview) package updates.
我现在已将ASP.NET Core项目更新为.NET 4.7,因此版本为两种解决方案都相同,但没有更改生成的错误。 csproj看起来像这样:
I have now updated the ASP.NET Core project to .NET 4.7, so that the version is the same on both solutions, but didn't change the error generated. The csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject>Practia.CrudGenerator.Web.Program</StartupObject>
</PropertyGroup>
<ItemGroup>..NUGET PACKAGES...</ItemGroup>
</Project>
推荐答案
问题是通过在尝试之前添加这两行来解决的发出结果:
The problem was solved by adding these two lines before attempting to emit the result:
compilation = compilation.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
compilation = compilation.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
请注意, AddReferences
和 WithOptions
返回新的 Compilation
实例,因此有必要重新分配。
Notice that both AddReferences
and WithOptions
return new Compilation
instances, so it is necessary to re-assign.
这篇关于通过Roslyn编译时找不到“主要”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!