20

I'm trying to build my docker image but i'm getting this error.

error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/6GAG.WebApi/6GAG.WebApi.csproj]

I have 3 projects in 1 solution.

  • 1 webapi
  • 1 frontend application
  • 1 class library

My Dockerfile exist in the directory where my .sln file is

FROM mcr.microsoft.com/dotnet/sdk:6.0 as build-env
WORKDIR /app

COPY 6GAG.WebApi/6GAG.WebApi.csproj /6GAG.WebApi/6GAG.WebApi.csproj 
COPY 6GAG.Core/6GAG.Core.csproj /6GAG.Core/6GAG.Core.csproj
RUN dotnet restore /6GAG.WebApi/6GAG.WebApi.csproj

COPY ./ ./
RUN dotnet publish /6GAG.WebApi/6GAG.WebApi.csproj -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
EXPOSE 80
COPY --from=build-env app/out/ .
ENTRYPOINT ["dotnet", "6GAG.WebApi.dll"]

My .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>_6GAG.WebApi</RootNamespace>
    <UserSecretsId>7f7e2bd0-6f27-4752-afe8-9839b765d3f0</UserSecretsId>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="10.1.1" />
    <PackageReference Include="FluentValidation.AspNetCore" Version="10.3.4" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\6GAG.Core\6GAG.Core.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Migrations\" />
  </ItemGroup>


</Project>

how can i fix this issue?

A.Creupelandt
  • 203
  • 1
  • 2
  • 6
  • If it is a .net core project, for sure you must have the Main function in Program.cs. It will not run otherwise without a Main function. – D A Nov 11 '21 at 12:41
  • @A.Creupelandt web app projects have a Program.cs and a Main as well, which builds up the web host and runs it. All .NET Core applications start as console applications. In .NET 6 with top-level statements, the entire `Program.cs` file is essentially the `Main` method. Does your project target .NET 6 though? What are the contents of the `csproj` file? – Panagiotis Kanavos Nov 11 '21 at 15:39
  • it's an aspnet core project. it builds when i build it locally – A.Creupelandt Nov 11 '21 at 15:44
  • @PanagiotisKanavos i added the content of the .csproj file – A.Creupelandt Nov 11 '21 at 15:49

6 Answers6

7

[Working solution below]

I experienced this kind of error on dotnet 6 as well. Since this is an upgrade to the previous version of dotnet core, I don't want to manually add a Main method in my program.cs. Are there alternatives?

cs.proj

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

Update! This worked for me

Getting "Program does not contain a static 'Main' method suitable for an entry point" when building using docker, why?

Docker - failed to compute cache key: not found - runs fine in Visual Studio

//Move the dockerfile to the .sln directory

Dipo
  • 112
  • 9
6

The underlying issue is that it's not finding files where it's been instructed to. See the bit in your Dockerfile where it says

COPY . . ?

Move the

WORKDIR "/src/xxxxxx"

line from below it to above it.

This will copy the restored packages from the preceding line to correct directory before attempting the

RUN dotnet build "xxxxx.csproj" -c Release -o /app/build

and that should now work

Ash
  • 5,786
  • 5
  • 22
  • 42
  • It works, but I don't really understand why changing of sequence of workdir and copy helped. I did clean all images/containers, rebuild/clean project/solution - same result: i can run app from VS, but docker build failed... Why?? – Vitaliy Markitanov Sep 21 '22 at 15:58
  • @VitaliyMarkitanov cleaning images/containers won't help. In the original sequence, it's copying the restored packages from dotnet restore to /src/xxxxx/ directory in the container. You then need to set the WORKDIR to the location of the csproj file before running dotnet build OR you could point the `RUN dotnet build` command on xxxxxx/xxxxx.csproj instead of just xxxxx.csproj. – Ash Oct 28 '22 at 02:00
4

I was encountering the same issue. Even though the project file is marked with

<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>

You need to also add the MS build flag for linux in your dokerfile -r linux-x64 or the marker in your .csproj so your docker file looks in the correct sdk location.

RUN dotnet publish /6GAG.WebApi/6GAG.WebApi.csproj -c Release -o out -r linux-x64
Stephen Gilboy
  • 5,572
  • 2
  • 30
  • 36
Visualspark
  • 817
  • 1
  • 6
  • 12
  • 1
    This `-r` is no longer needed in new pipelines. I tried [Felipe's answer](https://stackoverflow.com/a/74792342/1659999) and it worked fine. – Fawad Raza Apr 16 '23 at 10:36
4

When you say

"Program does not contain a static 'Main' method suitable for an entry point"

does it say to which project in your solution this referrers to?

I have seen this before when you either add a console app to the project instead of a class library or you try to migrate a console app into a class library, the simple change is to edit the csproj file for the troublesome project.

Once in the .csproj file change

<OutputType>exe</OutputType>

to

<OutputType>library</OutputType>
Iain
  • 146
  • 4
2

Try to check if in the libs projects it is like this

<Project Sdk="Microsoft.NET.Sdk.Web"> 

and change it to that

<Project Sdk="Microsoft.NET.Sdk">

In library projects, I referenced

Microsoft.AspNetCore.Mvc.Abstractions 

to solve my dependencies.

That worked for me.

1

I have 2 projects, one inside a solution, and the other one a class library, that has it's own repo on git, and it's acctually located on the same root off the solution folder. So my solution, to the same problema, was related to the COPY . . operation.

root
┣ Solution
┃ ┗ src/MyProject/
┃   ┗ MyProject.csproj
┣ Class
┃ ┗ class.csproj
┣ .dockerignore
┣ dockerfile

Since MyProject is really away from the ClassLib, I wasn't sending everything on the proper maner.

1st: I needed to alter the COPY["",""] operations, 2nd: manage the references, 3rd: at the end use the correct COPY operation to send everything to the correct destination folder.

The solution:

COPY ./Solution/src/ .
COPY ./ClassLib/ ./ClassLib

If you want the see the full dockerfile (Of course that MyProject is not the real name (= )

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["Solution/src/MyProject/MyProject.csproj", "MyProject/"]
COPY ["ClassLib/ClassLib.csproj", "ClassLib/"]
# Maneuver over references
WORKDIR /src/MyProject
RUN dotnet remove reference ../../../ClassLib/ClassLib.csproj
RUN dotnet add reference ../ClassLib/ClassLib.csproj
WORKDIR /src
#End Maneuver
RUN dotnet restore "MyProject/MyProject.csproj"

COPY ./Solution/src/ .
COPY ./ClassLib/ ./ClassLib

WORKDIR "MyProject"

RUN dotnet build "MyProject.csproj" -c Release -o /app/build


FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.dll"]