Peter
Peter

Reputation: 5332

Asp.Net Core: Program does not contain a static 'Main' method suitable for an entry point

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.

Upvotes: 29

Views: 32842

Answers (9)

sunnamed
sunnamed

Reputation: 233

In my case, it helped me to just remove from .csproj remove these lines:

<ItemGroup>
  <Compile Remove="Program.cs" />
</ItemGroup>
<ItemGroup>
  <Content Include="Program.cs" />
</ItemGroup>

Upvotes: -1

Ram Fattah
Ram Fattah

Reputation: 418

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

        }
    }
}

Upvotes: -1

NdamuleloNemakh
NdamuleloNemakh

Reputation: 3791

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

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <OutputType>Library</OutputType>
</PropertyGroup>

Upvotes: 48

Huseyin Durmus
Huseyin Durmus

Reputation: 440

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

enter image description here

Upvotes: 16

<PropertyGroup>
<LangVersion>latest</LangVersion>
    <OutputType>Library</OutputType>
  </PropertyGroup>

Upvotes: -2

doganak
doganak

Reputation: 798

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>

Upvotes: 4

ppenchev
ppenchev

Reputation: 157

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+

Upvotes: -1

jspinella
jspinella

Reputation: 2423

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!

Upvotes: -1

Sanket
Sanket

Reputation: 20037

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

Upvotes: 9

Related Questions