asp.net-core - 知道为什么在 F# 中跳过 C# 扩展调用吗?

标签 asp.net-core f# extension-methods 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

最佳答案

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

services.AddSingleton<IHostedService, CommandConsumer>Func<unit, IServiceCollection> ,这意味着它是一个尚未被调用的函数。

ignore然后接收该函数而不用它做任何事情。

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

关于asp.net-core - 知道为什么在 F# 中跳过 C# 扩展调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150723/

相关文章:

c# - 启动 Kestrel ASP.NET 服务器的证书问题 - Windows 10、C#、VS2019

asp.net-core - 从 ASP.NET 5 引用 .NET 4.6 项目会导致生成错误

f# - LexBuffer<char>.LexemeString 类型中的 "arg00"是什么?

c# - 这个 C# 扩展方法是否不纯,如果是,代码是否错误?

c# - 将 List<T> 定制为 ListCustomMinIndexedForEach<T> 的问题(从不相关的实现中剥离)

c# - 是否可以创建构造函数扩展方法?如何?

c# - 在 Net Core Web 应用程序中正确异步读取 Http 请求正文

c# - 部署 .NET Core 站点时,Ajax 调用返回 401

indexing - ( F#) 索引字符串而不对字符串进行类型注释

multithreading - 异步 F# 与 CCR 框架