有人可以告诉我在 F# 中编写异步 http 处理程序时使用的好模式吗?
我必须实现IHttpAsyncHandler
并且该接口需要一个BeginProcessRequest
and EndProcessRequest
。
我要回来Async.StartTask
吗?我该如何处理state:obj
和AsyncCallback
?
突然想到:使用异步工作流实现处理程序,然后使用 Async.AsBeginEnd 公开它
open System
open System.Web
type HttpAsyncHandler() =
let processRequestAsync (context : HttpContext) = async {
// TODO: implement
return()
}
let beginAction, endAction, _ = Async.AsBeginEnd(processRequestAsync)
interface IHttpAsyncHandler with
member this.BeginProcessRequest(context, callback, extraData) = beginAction(context, callback, extraData)
member this.EndProcessRequest(iar) = endAction (iar)
// other members omitted
你到底想做什么?如果可以,您可能需要HttpMessageHandler
考虑System.Net.Http
. 您只需覆盖
protected abstract Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken);
method, which is easy with an async { ... } |> Async.StartAsTask
. You also get better access to the various attributes of HTTP through static typing. Various subclasses allow you to run either through ASP.NET, WCF (self-host), or even third-part platforms like OWIN.