26

I am trying to port my Asp.Net WebApi project, based on the Onion Architecture design approach, over to Asp.Net Core. However, when I build my class libraries, the compiler is looking for the static Main method in Program.cs and I am getting:

C:\Projects\Some\src\Some.Core\error CS5001: Program does not contain a static 'Main' method suitable for an entry point

I am assuming that there should be only one Program.cs / entry point for the overall solution, and that is sitting inside of my WebApi project. Am I incorrect? Otherwise, how do I resolve this error? I falsely assumed that "emitEntryPoint": true served this purpose.

Here is an example of my class library's project.json:

{
  "version": "1.0.0-*",
  "description": "Some.Core Class Library",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Routing": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.Identity": "1.0.0",
    "Microsoft.Extensions.Configuration": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },
  "runtimes": {
    "win7-x64": {}
  }
}

Suggestions appreciated.

Peter
  • 5,251
  • 16
  • 63
  • 98

9 Answers9

39

Solved a similar issue by explicitly setting the OutputType tag in my classlib.csproj file:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <OutputType>Library</OutputType>
</PropertyGroup>
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
ENDEESA
  • 3,373
  • 2
  • 21
  • 17
  • Bingo! Hard to understand why `dotnet new classlib` doesn't put that in the `.csproj` file, or why the skeleton created by `dotnet new classlib` compiles but once I make a few changes to actually provide some code, it doesn't, but this is good enough for me. – Auspex May 27 '22 at 17:03
  • saved my day. still – samba2 Jun 09 '23 at 09:15
  • Project Properties > Application > General > Output type – Rami Alloush Jul 24 '23 at 20:30
11

I updated the output type of non-startup projects in solution to class library, and the problem was fixed.

enter image description here

Community
  • 1
  • 1
Huseyin Durmus
  • 380
  • 6
  • 14
  • 2
    Thanks for this comment, this was exactly the issue I was having. I had an extra console app which was not the startup project and after changing it's output to class library it all started working – Felipe Suárez Aug 06 '20 at 17:44
9

To avoid the error of "Program does not contain a static 'Main' method suitable for an entry point" in class library, Remove emitEntryPoint from buildOptions-

  "buildOptions": {
    "emitEntryPoint": true
  },

emitEntryPoint tells the compiler whether to create a Console Application or a Library. For more info refer this post

Community
  • 1
  • 1
Sanket
  • 19,295
  • 10
  • 71
  • 82
4

If the Main method has an async modifier, make sure that the selected C# language version is 7.1 or higher. You can fix the issue by adding below element to the .csproj file manually. Reference

<PropertyGroup>
  <LangVersion>latest</LangVersion>
</PropertyGroup>
doganak
  • 798
  • 14
  • 31
-1

I have been dealing with this error on macOS with .NET Core 2.0, and it appears to be related to my storing my solution in a Google Drive folder. It's certainly not because I am missing main methods in my projects, although intermittently I am seeing files/folders disappear from Rider and re-adding them (add existing item) is resolving the issue. I get the same error in VS for Mac and VS Code, but they don't seem to be updating the folder contents to reflect missing files, so it was harder to pinpoint the issue.

tl;dr Try moving your solution/project out of a synced network drive!

jspinella
  • 2,013
  • 3
  • 25
  • 39
-1

One obvious solution is to check the official MSDN error code description here

I had async modifier and had to move to C# language version 7.1+

ppenchev
  • 127
  • 1
  • 10
-1

Not sure if this is the correct solution;

Faced this error using .NET 6 with console app and added class library project (for unit testing) referenced to the console app.

In the console app, I added the following Main method in Program.cs file and the issue was solved:

namespace your_namespace
{
    internal class Program
    {
        public static void Main(string[] args)
        {

        }
    }
}

Ram Fattah
  • 349
  • 2
  • 11
-1

In .csproj remove these lines:

<ItemGroup>
  <Compile Remove="Program.cs" />
</ItemGroup>
<ItemGroup>
  <Content Include="Program.cs" />
</ItemGroup>
sunnamed
  • 96
  • 1
  • 10
-2
<PropertyGroup>
<LangVersion>latest</LangVersion>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  • 1
    Code-only answers, while they might solve the question at hand, are less good, because they generally do not describe why it's a good solution. You can improve your answers by reading this introduction to good answers: https://stackoverflow.com/help/how-to-answer –  Apr 18 '20 at 15:26