Docker .Net 6 error Program does not contain a static 'Main' method suitable for an entry point

Viewed 8123

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?

5 Answers

[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

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

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>

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.

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

Related