Last active
April 24, 2023 17:53
-
-
Save panesofglass/a1cfc3c9a3f0d41233ad to your computer and use it in GitHub Desktop.
How to use base.SendAsync in F# DelegatingHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type internal AsyncCallableHandler(messageHandler) = | |
inherit DelegatingHandler(messageHandler) | |
member internal x.CallSendAsync(request, cancellationToken) = | |
base.SendAsync(request, cancellationToken) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let loggingHandler = | |
{ new DelegatingHandler() with | |
member x.SendAsync(request, cancellationToken) = | |
let wrapped = new AsyncCallableHandler(base.InnerHandler) | |
let workflow = async { | |
let! requestContent = | |
request.Content.ReadAsStringAsync() | |
|> Async.AwaitTask | |
log requestContent | |
let! response = | |
wrapped.CallSendAsync(request, cancellationToken) | |
|> Async.AwaitTask | |
let! responseContent = | |
response.Content.ReadAsStringAsync() | |
|> Async.AwaitTask | |
log responseContent | |
return response | |
} | |
Async.StartAsTask(workflow, cancellationToken = cancellationToken) | |
} |
awesome! this doesnt work instead (or it's buggy as it works only if request is not changed)
let loggingHandler =
{ new DelegatingHandler() with
member x.SendAsync(request, cancellationToken) =
let sendBase = base.SendAsync(request, cancellationToken)
let workflow = task {
// your code here, if you change Request it will not change! beware !!!!!
return! sendBase
This should maybe be added to the official docs of delegating handlers for F#, without it, e.g. using base.SendAsync even inside a task, the task will not modify the request object, for example, I had a bug related to this in some AspNet code! awesome. Thank you so much @panesofglass 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!