2

For some reasons the line services.AddSingleton<IHostedService, CommandConsumer> |> ignore in let configureServices (services : IServiceCollection) is skipped while I am debugging my F# Giraffe application.

What is a bit frustrating is that the program compile so I would expect that the program being able stop at the breakpoint on that line, however it does not.

Weird thing is that if I am using the full namespace of the extension method then the breakpoint works... Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<IHostedService, CommandConsumer>(services) |> ignore

The source code in 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
Natalie Perret
  • 8,013
  • 12
  • 66
  • 129

2 Answers2

6

The line services.AddSingleton<IHostedService, CommandConsumer> |> ignore is incorrect syntax, which you can see if you break down the types involved.

services.AddSingleton<IHostedService, CommandConsumer> is Func<unit, IServiceCollection>, meaning it's a function that has yet to be invoked.

ignore is then receiving that function and not doing anything with it.

The syntax you actually want in order to register a singleton to services is services.AddSingleton<IHostedService, CommandConsumer>() |> invoke, meaning 'execute the AddSingleton call and ignore the return value.

Chester Husk
  • 1,408
  • 12
  • 14
1

You can use services.AddHostedService(CommandConsumer) |> ignore instead of services.AddSingleton<IHostedService, CommandConsumer>() |> ignore and the CommandConsumer will live inside a singleton that hosts your background services. See this question for discussion on the topic.

cmo
  • 136
  • 2
  • 10