C Interoperability between an F# asynchronous workflow and .NET Task

 

Despite the similarities between the asynchronous programming models exposed by C# and F# programming languages, their interoperability isn’t a trivial accomplishment. F# programs tend to use more asynchronous computation expressions than .NET Task. Both types are similar, but they do have semantic differences, as shown in chapters 7 and 8. For example, tasks start immediately after their creation, while F# Async must be explicitly started.

How can you interop between F# asynchronous computation expressions and .NET Task? It’s possible to use F# functions such as Async.StartAsTask<T> and Async.AwaitTask<T> to interop with a C# library that returns or awaits a Task type.

Conversely, there are no equivalent methods for converting an F# Async to a Task type. It would be helpful to use the built-in F# Async.Parallel computation in C#. In this listing, repeated from chapter 9, the F# downloadMediaAsyncParallel function downloads asynchronously in parallel images from Azure Blob storage.