visual-studio - F# docker 应用程序 : A function labeled with the 'EntryPointAttribute' attribute must be the last declaration in the last file. ...?

标签 visual-studio docker f# .net-core

可以通过从头开始创建新项目轻松重现该问题。

Visual Studio 版本为 V15.7.5。我创建了一个新的 F# .Net core 2.1 应用程序,支持 docker (Linux)。但是,它出现了以下错误。

A function labeled with the 'EntryPointAttribute' attribute must be the last declaration in the last file in the compilation sequence.

以下是Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 64890
EXPOSE 44390

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY WebApplication8/WebApplication8.fsproj WebApplication8/
RUN dotnet restore WebApplication8/WebApplication8.fsproj
COPY . .
WORKDIR /src/WebApplication8
RUN dotnet build WebApplication8.fsproj -c Release -o /app

FROM build AS publish
RUN dotnet publish WebApplication8.fsproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication8.dll"]

下面是Program.fs

namespace WebApplication8

open System
open System.Collections.Generic
open System.IO
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Logging

module Program =
    let exitCode = 0

    let CreateWebHostBuilder args =
        WebHost
            .CreateDefaultBuilder(args)
            .UseStartup<Startup>();

    [<EntryPoint>]
    let main args =
        CreateWebHostBuilder(args).Build().Run()
        exitCode

.fsproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
    <UserSecretsId>b379991e-83d8-444e-b888-62e9ac139864</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Controllers/ValuesController.fs" />
    <Compile Include="Startup.fs" />
    <Compile Include="Program.fs" />
  </ItemGroup>

  <ItemGroup>
    <None Include="Dockerfile" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

完整的错误,

1>------ Build started: Project: docker-compose, Configuration: Debug Any CPU ------
1>docker ps --filter "status=running" --filter "name=dockercompose15479956486365536117_webapplication8_" --format {{.ID}} -n 1
1>e95714bd0022
1>docker exec -i e95714bd0022 /bin/bash -c "if PID=$(pidof -x dotnet); then kill $PID; fi"
1>c:\source\repos\WebApplication8\WebApplication8\WebApplication8.fsproj : warning NU1701: Package 'System.Linq.Queryable 4.0.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.
1>c:\source\repos\WebApplication8\WebApplication8\Program.fs(22,9): error FS0433: A function labeled with the 'EntryPointAttribute' attribute must be the last declaration in the last file in the compilation sequence.
1>Done building project "WebApplication8.fsproj" -- FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

start.fs generated by VS

namespace WebApplication8

open System
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.HttpsPolicy;
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection

type Startup private () =
    new (configuration: IConfiguration) as this =
        Startup() then
        this.Configuration <- configuration

    // This method gets called by the runtime. Use this method to add services to the container.
    member this.ConfigureServices(services: IServiceCollection) =
        // Add framework services.
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1) |> ignore

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    member this.Configure(app: IApplicationBuilder, env: IHostingEnvironment) =
        if (env.IsDevelopment()) then
            app.UseDeveloperExceptionPage() |> ignore
        else
            app.UseHsts() |> ignore

        app.UseHttpsRedirection() |> ignore
        app.UseMvc() |> ignore

    member val Configuration : IConfiguration = null with get, set

VS生成的value.fs

namespace WebApplication8.Controllers

open System
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore.Mvc

[<Route("api/[controller]")>]
[<ApiController>]
type ValuesController () =
    inherit ControllerBase()

    [<HttpGet>]
    member this.Get() =
        let values = [|"value1"; "value2"|]
        ActionResult<string[]>(values)

    [<HttpGet("{id}")>]
    member this.Get(id:int) =
        let value = "value"
        ActionResult<string>(value)

    [<HttpPost>]
    member this.Post([<FromBody>] value:string) =
        ()

    [<HttpPut("{id}")>]
    member this.Put(id:int, [<FromBody>] value:string ) =
        ()

    [<HttpDelete("{id}")>]
    member this.Delete(id:int) =
        ()

最佳答案

.fsproj 中的 UserSecretsId 标记导致了问题,因为 .net 核心编译器在错误的位置为其生成代码。解决方案是删除标记并将其添加到单独的 assemblyinfo.fs 文件中:

module Your.Namespace.AssemblyInfo

open Microsoft.Extensions.Configuration.UserSecrets

[<assembly: UserSecretsIdAttribute(“b379991e-83d8-444e-b888-62e9ac139864”)>]

do()

有关问题和解决方法的更详细描述,请参阅本文:Making ASP.NET Core User Secrets Work in F# Projects

关于visual-studio - F# docker 应用程序 : A function labeled with the 'EntryPointAttribute' attribute must be the last declaration in the last file. ...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51622669/

相关文章:

Docker NodeJS bcrypt 错误

f# - 为什么将 ref 类型传递到需要 byref 的 F# 函数会出现类型错误?

c# - 对象、方法等的应用程序范围限制

.net - 将exe文件放入构建输出目录

visual-studio - VIsual Studio 2010,轻松最大化/最小化编辑器?

docker - 由于 Luigi 的工作分配不均, worker 过早死亡 (2.6.1)

visual-studio - 如何在 Visual Studio 中分别在不同文件夹/项目中运行(单元)测试?

tomcat - 如何从家庭网络中的另一台 LAN pc 访问 docker 容器

f# - 如何将相同的参数传递给多个函数?

exception - 为什么 F# failedwith 返回泛型类型而不是异常类型