我想用Suave做一个简单的计数器。
[<EntryPoint>]
let main argv =
let mutable counter = 0;
let app =
choose
[
GET
>=> choose
[
path "/" >=> OK "Hello, world. ";
path "/count" >=> OK (string counter)
]
POST
>=> choose
[
path "/increment"
>=> (fun context -> async {
counter <- counter + 1
return Some context
})
]
]
startWebServer defaultConfig app
0然而,在我目前的解决方案中,/count的计数从不更新。
我认为这是因为WebPart是在应用程序启动时计算的,而不是为每个请求计算的。
在Suave中实现这一点的最佳方法是什么?
发布于 2018-12-31 21:20:08
你的假设是正确的,Webparts是值,所以只计算一次。(参见this)。
你需要使用闭包来获得你想要的东西:
path "/count" >=> (fun ctx ->
async {
let c = counter in return! OK (string c) ctx
})https://stackoverflow.com/questions/53987587
复制相似问题