CR Sardar
CR Sardar

Reputation: 1026

Dockerizing ASP .NET Core App, CS5001: Program does not contain a static 'Main' method

New to .NET Core, created a program/project with Program.cs file as

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace MyServer
{
    static class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddCors((o) => o.AddPolicy("AllowAllOrigins",
                builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyHeader();
                    builder.AllowAnyMethod();
                }));
            
            builder.Services.AddHttpContextAccessor();
            
            builder.Services.AddMyServerForHttp(builder.Configuration);

            var app = builder.Build();

            app.UseCors("AllowAllOrigins");
            app.Use(async (context, next) =>
            {
                if (context.Request.Method == "GET")
                {
                    // Added for testing purposes.
                    await context.Response.WriteAsync("Hello!");
                }
                else
                {
                    await next.Invoke();
                }
            });
            
            app.Run();
        }
    }

}

It is running file when I test is using command lines like -

dotnet restore my-project.csproj
dotnet build my-project
dotnet publish my-project
dotnet bin/Debug/net6.0/my-project.dll

But, when I am trying to create a Docker image of it, it is giving bellow error -

docker build . -t my-project-0.0.1_2023-05-12

..... 13.3s => ERROR [build 5/5] RUN dotnet build "my-project.csproj" -c Release -o /app 4.3s ------
> [build 5/5] RUN dotnet build "my-project.csproj" -c Release -o /app:
#9 0.462 MSBuild version 17.3.2+561848881 for .NET
#9 1.139 Determining projects to restore...
#9 1.551 All projects are up-to-date for restore.
#9 4.196 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/app/my-project.csproj] #9 4.207 #9 4.207 Build FAILED. #9 4.207 #9 4.207 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/app/my-project.csproj] #9 4.207 0 Warning(s) #9 4.207 1 Error(s) #9 4.207 #9 4.207 Time Elapsed 00:00:03.63 ------ executor failed running [/bin/sh -c dotnet build "my-project.csproj" -c Release -o /app]: exit code: 1

My Docker file is like -

FROM mcr.microsoft.com/dotnet/aspnet:6.0-cbl-mariner2.0 AS base

RUN tdnf update -y && tdnf install -y gcc-multilib && tdnf install -y libcurl3-gnutls
#ENV LD_DEBUG libs
WORKDIR /app
EXPOSE 5000
EXPOSE 8080

FROM mcr.microsoft.com/dotnet/sdk:6.0-cbl-mariner2.0 AS build

WORKDIR /app
COPY . .
RUN dotnet restore "my-project.csproj"

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

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

ENTRYPOINT ["dotnet", "bin/Debug/net6.0/my-project.dll"]

What is the problem here? I already have ma Main method.

Upvotes: 1

Views: 562

Answers (1)

Gopi
Gopi

Reputation: 102

May be problem is with image selection try with aspnet:6.0
I have configured my docker like below and it was working well and good

FROM mcr.microsoft.com/dotnet/aspnet:5.0
RUN apt-get update && \
    apt-get install -y libc6 -f -o APT::Immediate-Configure=0 && \
    apt-get install -y \
        libgdiplus \
        libicu-dev \
        libharfbuzz0b \
        libfontconfig1 \
        libfreetype6 \
        libpango-1.0-0 \
        libpangocairo-1.0
        
WORKDIR /app

COPY ./Output .
ENTRYPOINT ["dotnet", "Reporting.Web.dll"]

Upvotes: 0

Related Questions