由于某些原因,该行
services.AddSingleton<IHostedService, CommandConsumer> |> ignore
在 let 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/