【问题标题】:Docker .Net 6 error Program does not contain a static 'Main' method suitable for an entry pointDocker .Net 6 错误程序不包含适用于入口点的静态“主要”方法
【发布时间】:2021-11-11 12:37:07
【问题描述】:

我正在尝试构建我的 docker 映像,但出现此错误。

错误 CS5001:程序不包含适用于入口点的静态“Main”方法 [/6GAG.WebApi/6GAG.WebApi.csproj]

我在 1 个解决方案中有 3 个项目。

  • 1 个 webapi
  • 1 个前端应用程序
  • 1个类库

我的 Dockerfile 存在于我的 .sln 文件所在的目录中

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"]

我的 .csproj 文件:

<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>

我该如何解决这个问题?

【问题讨论】:

  • 如果是.net core项目,一定要有Program.cs中的Main函数。如果没有 Main 函数,它将不会运行。
  • @A.Creupelandt Web 应用程序项目也有一个 Program.cs 和一个 Main,它构建了 Web 主机并运行它。所有 .NET Core 应用程序都作为控制台应用程序启动。在具有顶级语句的 .NET 6 中,整个 Program.cs 文件本质上是 Main 方法。您的项目是否以 .NET 6 为目标? csproj 文件的内容是什么?
  • 这是一个 aspnet 核心项目。当我在本地构建它时它会构建
  • @PanagiotisKanavos 我添加了 .csproj 文件的内容

标签: asp.net-core dockerfile asp.net-core-webapi .net-6.0


【解决方案1】:

我遇到了同样的问题。即使项目文件标有

<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>

您还需要在您的 dockerfile -r linux-64 或 .csproj 中添加 linux 的 MS 构建标志,以便您的 docker 文件在正确的 sdk 位置中查找。

RUN dotnet publish /6GAG.WebApi/6GAG.WebApi.csproj -c Release -o out -r linux-64

【讨论】:

    【解决方案2】:

    [下面的工作解决方案]

    我在 dotnet 6 上也遇到过这种错误。由于这是对以前版本的 dotnet core 的升级,我不想在我的 program.cs 中手动添加 Main 方法。有替代品吗?

    cs.proj

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

    更新! 这对我有用

    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

    //将dockerfile移动到.sln目录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 2013-06-10
      • 2013-04-14
      • 2014-12-13
      • 1970-01-01
      • 2018-05-02
      相关资源
      最近更新 更多