' Main'通过Roslyn编译时找不到方法 [英] 'Main' method not found when compiling through Roslyn

查看:66
本文介绍了' Main'通过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:程序不包含适合的静态"Main"方法进入点

可能是所使用的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.

这篇关于&amp;#39; Main&amp;#39;通过Roslyn编译时找不到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆