不包含适合运行 docker build 时的入口点的静态“main”方法

问题描述 投票:0回答:1

不包含适合入口点的静态“main”方法

using CountryAPI.Models;

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

// Add services to the container.
        builder.Services.AddControllers();
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();
        builder.Services.AddSingleton<ICountryService, CountryService>(); // Singleton service

        var app = builder.Build();


// Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseAuthorization();
        app.MapControllers();
        app.Run();

    }
}

程序在 Visual Studio 中运行,但在终端中使用 docker build 命令时显示 CSC 5001 错误。

  1. 尝试将输出类型更改为exe
  2. 确保程序处于编译模式
  3. 添加了一个main方法
  4. 一切都重新开始

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:8.0-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0-nanoserver-1809 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["CountryAPI.csproj", "CountryAPI/"]
RUN dotnet restore "./CountryAPI/./CountryAPI.csproj"
COPY . .
WORKDIR "/src/CountryAPI"
RUN dotnet build "./CountryAPI.csproj" -c %BUILD_CONFIGURATION% -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./CountryAPI.csproj" -c %BUILD_CONFIGURATION% -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CountryAPI.dll"] 
c# docker asp.net-web-api dockerfile
1个回答
0
投票

将 .csproj 中的 OutputType 更改为 Library 有效

<OutputType>Library</OutputType>
© www.soinside.com 2019 - 2024. All rights reserved.