'Main' method not found when compiling through Roslyn

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 05:35:15

问题


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:

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

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();
    }
}

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 fails with:

  • warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
  • error CS5001: Program does not contain a static 'Main' method suitable for an entry point

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.

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>

回答1:


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));

Notice that both AddReferences and WithOptions return new Compilation instances, so it is necessary to re-assign.



来源:https://stackoverflow.com/questions/45821851/main-method-not-found-when-compiling-through-roslyn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!