【问题标题】:Program does not contain a static 'Main' method suitable for an entry point [/src/Main/Api/Api.csproj] in Azure DevOps程序不包含适用于 Azure DevOps 中的入口点 [/src/Main/Api/Api.csproj] 的静态“Main”方法
【发布时间】:2020-12-14 11:41:31
【问题描述】:

我知道这个问题重复了很多次,但我尝试了许多已发布的解决方案,但没有任何效果。 所以我在 Azure DevOps 发布管道上运行 docker,代理作业返回了这个错误:

Program does not contain a static 'Main' method suitable for an entry point [/src/Main/Api/Api.csproj]

这是我的 Dockerfile:

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

ENV ASPNETCORE_ENVIRONMENT #{environment-profile}#

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["Main/Api/Api.csproj", "Main/Api/"]
COPY ["Main/Domain/Domain.csproj", "Main/Domain/"]
COPY ["Side/Infra/Infra.csproj", "Side/Infra/"]
RUN dotnet restore "Main/Api/Api.csproj"

WORKDIR "/src/Main/Api"
COPY . .
RUN dotnet build "Api.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.dll", "--environment=#{environment-profile}#"]

错误来自这一行:RUN dotnet build "Api.csproj" -c Release -o /app/build

这是我的项目结构:

打开.sln文件后:

  • 主要(文件夹)
    • API(文件夹)
      • Api.csroj
    • 域(文件夹)
      • Domain.csproj
  • 侧(文件夹)
    • 基础设施(文件夹)
      • Infra.csproj

我关注了 SO 中的一些帖子,将 COPY . . 放在 WORKDIR "/src/Main/Api" 之后,但效果不佳。

我也不想将我的 Dockerfile 移动到根目录,因为我在这个解决方案中有两个 Dockerfile,所以我想将它们维护在各自的文件夹中。 (不过我还是试过了,把 Dockerfile 放到根目录下也不行……)

此外,这个 Dockerfile 在我的本地成功构建。仅在 Azure DevOps 发布管道中失败。我已将发布管道中 Dockerfile 的路径设置为$(System.DefaultWorkingDirectory)/_Main_CI/drop/Main/Api/Dockerfile,并将构建上下文设置为$(System.DefaultWorkingDirectory)/_Main_CI/drop

如果有人知道出了什么问题,不胜感激!谢谢!

【问题讨论】:

  • 嗨@Corene,我注意到Bassam Gamal 与您分享了许多有用的信息。如果建议对您解决问题有帮助,请将他的回答标记为该主题的解决方案。在寻找答案时,它可能会帮助遇到类似问题的其他人。谢谢。

标签: docker .net-core azure-devops


【解决方案1】:

最终发现了问题,我需要将所有内容从我的 repo 复制到 Artifact,因为 Dockerfile 正在寻找 Program.cs 以找到 main 方法。而且我只复制了.csproj 文件,这使得它在构建图像时找不到Program.cs

【讨论】:

    【解决方案2】:

    在您的 docker build 步骤中,您需要控制执行 docker build 命令的上下文。在这种情况下应该是根文件夹,如果是**,那么它使用相同的dockerfile文件夹,对于root删除它们或手动选择文件夹。

    【讨论】:

    • 是的,我的 docker 文件路径是 $(System.DefaultWorkingDirectory)/_Main_CI/drop/Main/Api/Dockerfile,我的构建上下文路径是 $(System.DefaultWorkingDirectory)/_Main_CI/drop
    • 尝试两件事,使用 'docker system prune' 清除本地缓存并重建并查看它是否有效。如果您使用的是 VS/Code,则重新生成 dockerfile,也许您做了一些路径更改,因此模块缺少复制指令
    • 这个是在Azure DevOps上运行的,能清除缓存吗?生成的 dockerfile 再次完成,除了我为 ENV 变量添加之外,其他都是相同的。而COPY . .WORKDIR "/src/Main/Api" 前面
    • 一般情况下不用加复制指令,缓存是在本地测试是否有效,我以为是缓存了一层。
    • 是的,在本地我已经在容器中运行了映像,并且在我将流量路由到 8080 端口后能够访问 http://localhost:8080/ 中的 Api Swagger 服务,因为端口 80 在我的本地不可用。是端口问题吗?因为在 Dockerfile 中它被指定为 EXPOSE 80.
    猜你喜欢
    • 2021-02-19
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多