由于某些原因,生产线
在调试F#Giraffe应用程序时,会跳过services.AddSingleton<IHostedService, CommandConsumer> |> ignore
中的let configureServices (services : IServiceCollection)
。
令人沮丧的是该程序可以编译,因此我希望该程序能够在该行的断点处停止,但事实并非如此。
奇怪的是,如果我使用扩展方法的完整 namespace ,则断点可以工作...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/