Docker .Net 6 错误程序不包含适合入口点的静态“Main”方法

A.C*_*ndt 23 dockerfile asp.net-core asp.net-core-webapi .net-6.0

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

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

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

  • 1 个网络 API
  • 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"]
Run Code Online (Sandbox Code Playgroud)

我的 .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>
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Ash*_*Ash 12

根本问题是它没有找到指示的文件。查看 Dockerfile 中显示的部分

COPY . .

移动

WORKDIR "/src/xxxxxx"

线从其下方到其上方。

这会将恢复的包从前一行复制到正确的目录,然后再尝试

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

现在应该可以了


Dip*_*ipo 7

[下面的工作解决方案]

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

cs.项目

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

更新! 这对我有用

使用 docker 构建时出现“程序不包含适合入口点的静态‘Main’方法”,为什么?

Docker - 无法计算缓存键:未找到 - 在 Visual Studio 中运行良好

//将dockerfile移动到.sln目录

  • 发布了相同的解决方案,然后当我看到你的解决方案时将其删除。删除&lt;OutputType&gt;exe&lt;OutputType&gt;节点,将Dockfile移动到.sln目录下即可 (3认同)
  • 我不认为这是一个解决方案。将 dockerfile 移动到 .sln 目录会导致 VS 中的 Docker 启动配置文件失败。 (2认同)

Iai*_*ain 5

当你说

“程序不包含适合入口点的静态‘Main’方法”

它是否说明了该解决方案引用了哪个项目?

我以前见过这种情况,当您将控制台应用程序添加到项目而不是类库中,或者尝试将控制台应用程序迁移到类库中时,简单的更改就是为麻烦的项目编辑 csproj 文件。

一旦在 .csproj 文件中更改

<OutputType>exe</OutputType>
Run Code Online (Sandbox Code Playgroud)

<OutputType>library</OutputType>
Run Code Online (Sandbox Code Playgroud)