知道为什么在F#中跳过该C#扩展调用吗?

Eho*_*ret 2 extension-methods f# asp.net-core f#-giraffe

由于某些原因,services.AddSingleton<IHostedService, CommandConsumer> |> ignorelet configureServices (services : IServiceCollection)调试F#Giraffe应用程序时跳过了该行 。

令人沮丧的是该程序可以编译,因此我希望该程序能够在该行的断点处停止,但事实并非如此。

奇怪的是,如果我使用扩展方法的完整名称空间,则断点可以工作... Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<IHostedService, CommandConsumer>(services) |> ignore

中的源代码Program.fs

module Rm.Accounting.Workflows.Api.App

open System
open System.Threading
open System.Threading.Tasks
open System.IO
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open Giraffe
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Hosting

type CommandConsumer()=
    inherit BackgroundService()

    override __.ExecuteAsync(cancellationToken : CancellationToken) =
        Task.CompletedTask


let webApp = choose [
                GET >=>
                    choose [
                        route "/" >=> redirectTo false "/health"
                    ]
                setStatusCode 404 >=> text "Not Found" ]

let errorHandler (ex : Exception) (logger : ILogger) =
    logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
    clearResponse >=> setStatusCode 500 >=> text ex.Message

let configureApp (app : IApplicationBuilder) =
    app.UseHealthChecks(PathString("/health")) |> ignore
    let env = app.ApplicationServices.GetService<IHostingEnvironment>()
    (match env.IsDevelopment() with
    | true  -> app.UseDeveloperExceptionPage()
    | false -> app.UseGiraffeErrorHandler errorHandler)
        .UseHttpsRedirection()
        .UseStaticFiles()
        .UseGiraffe(webApp)

let configureServices (services : IServiceCollection) =
    // That line below stops the debugger if there a breakpoint.
    // Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<IHostedService, CommandConsumer>(services)
    // |> ignore
    // The line does not stop the debugger with a breakpoint while the one above can, weird.
    services.AddSingleton<IHostedService, CommandConsumer> |> ignore
    services.AddHealthChecks() |> ignore
    services.AddGiraffe()      |> ignore


let configureLogging (builder : ILoggingBuilder) =
    builder.AddFilter(fun l -> l.Equals LogLevel.Error)
           .AddConsole()
           .AddDebug() |> ignore

[<EntryPoint>]
let main _ =
    let contentRoot = Directory.GetCurrentDirectory()
    let webRoot     = Path.Combine(contentRoot, "WebRoot")
    WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(contentRoot)
        .UseIISIntegration()
        .UseWebRoot(webRoot)
        .Configure(Action<IApplicationBuilder> configureApp)
        .ConfigureServices(configureServices)
        .ConfigureLogging(configureLogging)
        .Build()
        .Run()
    0
Run Code Online (Sandbox Code Playgroud)

Che*_*usk 6

该行services.AddSingleton<IHostedService, CommandConsumer> |> ignore的语法不正确,如果您分解所涉及的类型,则可以看到该语法。

services.AddSingleton<IHostedService, CommandConsumer>Func<unit, IServiceCollection>,表示此函数尚未被调用。

ignore 然后接收该功能,而不对其执行任何操作。

为了向服务注册单例,您实际上想要的语法是services.AddSingleton<IHostedService, CommandConsumer>() |> invoke,这意味着“执行AddSingleton调用并忽略返回值。