I have to implement a C# interface with F#. The interface and many of the methods I have to use are all written with C# and use async very heavily.
Example code to port from C# to F#:
public async Task CloseAsync(PartitionContext context, CloseReason reason)
{
Console.WriteLine(string.Format("Processor Shuting Down. Partition '{0}', Reason: '{1}'.", this.partitionContext.Lease.PartitionId, reason.ToString()));
if (reason == CloseReason.Shutdown)
{
await context.CheckpointAsync();
}
}
This can be problematic, as F# and C# do not use the same async pattern or types. The function above is part of an interface that I need to implement. My issue comes in how do I await context.CheckpointAsync()
with F# in such a way that it still returns a Task
.
This is the direction I was attempting to go, however it does not work. context.CheckpointAsync
returns Task<a>
not Task
. Additionally, what about the other cases, in which no operation is expected?
interface IEventProcessor with
member this.CloseAsync(context:PartitionContext, reason:CloseReason) =
match reason with
| CloseReason.Shutdown -> context.CheckpointAsync() |> Async.AwaitTask
| _ -> ()