Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Main' method not found when compiling through Roslyn

Tags:

c#

.net

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:

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>
like image 674
Camilo Terevinto Avatar asked Aug 22 '17 15:08

Camilo Terevinto


Video Answer


1 Answers

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.

like image 51
Camilo Terevinto Avatar answered Nov 13 '22 04:11

Camilo Terevinto