简体   繁体   中英

Pattern for implementing async http handlers in F#?

Could someone tell me a good pattern to use when coding asynchronous http handlers in F#?

I have to implement IHttpAsyncHandler and that interface requires a BeginProcessRequest and EndProcessRequest .

Do I return Async.StartTask ? How do I handle state:obj and AsyncCallback ?

Off the top of my head: implement handler using async workflow and then expose it with 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

What are you trying to do, exactly? If you are able, you might want to consider the HttpMessageHandler and its ilk from System.Net.Http . You only have to override the

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM