运行 后台作业是否有通用的 f# 修道院方法

is there any generic f# convent way to run background job

在 c# 中,方便的方法是继承 BackgroundService class 并使用 AddHostedService() 注册到托管框架。

在 f# 中是否有任何通用的本机方法来执行此操作?不想在 f# fp 代码风格中混合 c# OO 代码风格

我没试过这个,所以买者自负,但避免创建 class 类型的一种方法是使用 object expression 代替:

let service =
    {
        new BackgroundService() with
            override __.ExecuteAsync(_) =
                new Task(fun () -> printfn "Hello world")
    }

我想你可以这样注册:

services.AddHostedService(fun _ -> service)