How to Debug an Async Block in F# with Visual Studio 2019 - f#

I am having trouble getting all the info I need while debugging an F# async block in Visual Studio
I can debug, breakpoint and step through the following code - but I cannot get the values of a variable after executing a line.
For example - I execute past the line that starts with: let! emailField =
and I cannot see the variable emailField anywhere. Any ideas appreciated.
let getLoggedInBrowser(email:string, password: string, initialPageUrl: string) =
async {
let! playwright = Async.AwaitTask(Playwright.CreateAsync())
let! browser = Async.AwaitTask(playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions(Headless=false)))
let! page = Async.AwaitTask(browser.NewPageAsync())
do! Async.AwaitTask(page.GotoAsync("https://go.xero.com/Dashboard/")) |> Async.Ignore
do! Async.AwaitTask(page.WaitForSelectorAsync("#xl-form-email")) |> Async.Ignore
let! emailField = Async.AwaitTask(page.FillAsync("#xl-form-email",email))
do! Async.AwaitTask(page.FillAsync("#xl-form-password",password))
do! Task.WhenAll(page.ClickAsync("#xl-form-submit"),page.WaitForNavigationAsync()) |> Async.AwaitTask
do! page.GotoAsync(initialPageUrl) |> Async.AwaitTask |> Async.Ignore
return playwright, browser , page
}

Related

await generic Task and return the result

I have function call
let result = session.Query<Domain.CustomerReadModel>().ToListAsync()
which returns Task<Collections.Generic.IReadOnlyList<Domain.CustomerReadModel>>
How do I "await" this task correctly in F#?
I tried
async {
session.Query<Domain.CustomerReadModel>().ToListAsync() |> ignore
}
as well as
let result = session.Query<Domain.CustomerReadModel>().ToListAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
This seems to compile but I can't use the result and return it:
session.Query<Domain.CustomerReadModel>().ToListAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
Async.AwaitTask takes the task, wraps it in an async computation, and returns you that computation. Once you have it, you can use it with let! or do! or return! just like any other async computation.
async {
let queryAsFsharpAsync = session.Query<Domain.CustomerReadModel>().ToListAsync() |> Async.AwaitTask
let! result = queryAsFsharpAsync
...
}
Or without giving the computation its own name:
async {
let! result = session.Query<Domain.CustomerReadModel>().ToListAsync() |> Async.AwaitTask
...
}

Calling C# method which returns a Task

This is some C# code:
var streamStore = new PostgresStreamStore(new PostgresStreamStoreSettings("Host=localhost;Port=5432;User Id=postgres;Password=123456;Database=postgres"));
await streamStore.CreateSchemaIfNotExists();
I'm trying to call it from F# like this:
let db_connection =
Sql.host "localhost"
|> Sql.port 5432
|> Sql.username "postgres"
|> Sql.password "123456"
|> Sql.database "postgres"
|> Sql.str
let store =
new PostgresStreamStore(PostgresStreamStoreSettings(db_connection))
store.CreateSchemaIfNotExists() |> Async.AwaitTask |> ignore
The code compiles, however the schema in contrast to the C# Version does not a create a schema.
How do I await this Task from store.CreateSchemaIfNotExists?
I'm getting this error message:
`This expression is a function value, i.e. is missing arguments. Its type is unit -> Tasks.Task.
In the C# code, you are using await, so this must be inside an async method. The corresponding thing in F# would be to use F# asynchronous workflows. Inside those, you can use let! which is similar to await. This works with computations of type Async<T> rather than Task<T>. The operation Async.AwaitTask turns Task<T> into Async<T> so that you can access it using let!
let doSomething () = async {
let db_connection =
Sql.host "localhost"
// (other configuration omitted)
let store =
new PostgresStreamStore(PostgresStreamStoreSettings(db_connection))
let! res = store.CreateSchemaIfNotExists() |> Async.AwaitTask
return "whatever" }
I assume that CreateSchemaIfNotExists does not return anything useful, so you can also wait for its completion using do!
do! store.CreateSchemaIfNotExists() |> Async.AwaitTask |> Async.Ignore
An asynchronous computation then needs to be started using Async.Start or Async.RunSynchronously, which is akin to starting a task or blocking using task.RunSynchronously.

F# async function containing while loop with async calls fail to compile

Not really sure why my code fails to compile, the error I am having:
Incomplete structured construct at or before this point in expression
let resendErrorsAsync (bus: IBus) (errorQueueName: string) =
async {
let! errorQueue = bus.Advanced.QueueDeclareAsync(errorQueueName) |> Async.AwaitTask
let! message = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
while message <> null do
let utf8Body = Encoding.UTF8.GetString(message.Body)
let error = JsonConvert.DeserializeObject<Error>(utf8Body)
let errorBodyBytes = Encoding.UTF8.GetBytes(error.Message)
let! exchange = bus.Advanced.ExchangeDeclareAsync(error.Exchange, "topic") |> Async.AwaitTask
let! message = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
}
It seems to be related to my two async calls in the while loop, not really sure why though.
You can't end a block with a let!, there has to be a body after it; that's the reason for the syntax error.
The message you're defining on the last line is a different variable to the one tested in the while condition. It seems you want to mutate the message; for that, you have to explicitly create a mutable variable:
let resendErrorsAsync (bus: IBus) (errorQueueName: string) =
async {
let! errorQueue = bus.Advanced.QueueDeclareAsync(errorQueueName) |> Async.AwaitTask
let! msg = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
let mutable message = msg
while message <> null do
let utf8Body = Encoding.UTF8.GetString(message.Body)
let error = JsonConvert.DeserializeObject<Error>(utf8Body)
let errorBodyBytes = Encoding.UTF8.GetBytes(error.Message)
let! exchange = bus.Advanced.ExchangeDeclareAsync(error.Exchange, "topic") |> Async.AwaitTask
let! msg = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
message <- msg
}

How to convert the download program to async?

I have the following code
open FSharp.Data
let downloadFile link =
......
use os = File.Create(...)
Http.RequestStream(....).ReponseStream.CopyTo(os)
let rec consume() = async {
......
|> Seq.iter (fun x ->
xxx |> Seq.iter(fun link ->
downloadFile link
))
}
I found that the sync downloading makes the code not run concurrently. So I'm trying to do somthing like the following. How to change it to use the FSharp.Data http AsyncRequestStream? Maybe the CopyTo can be async too?
open FSharp.Data
let downloadFile link = async {
......
use os = File.Create(...)
Http.AsyncRequestStream(....).ReponseStream.CopyTo(os) // Error
}
let rec consume() = async {
......
|> Seq.iter (fun x ->
xxx |> Seq.iter(fun link ->
downloadFile link |> Async.Start // do! downloadFile link????
))
}
consume() |> Async.RunSynchronously
Here's a skeleton solution, worthy of all the blank spots in your example:
let downloadFile link =
async {
......
use os = File.Create(...)
let! resp = Http.AsyncRequestStream(....)
return resp.ReponseStream.CopyTo(os)
}
let consume link =
async {
let comps : Async<unit> [] =
xxx
|> Seq.map (fun link -> downloadFile link)
|> Array.ofSeq
return! Async.Parallel comps
}
I think you should read up on asynchronicity and concurrency in general, as well as how to use it in F# in particular. From the OP it seems the whole thing is a bit hazy to you.
Edit: to answer the question in the comment:
With return! (or let!, or do!) you execute the nested workflow asynchronously, then pick up executing the current workflow from that point. That is, everything "below" the do! is put into a continuation that gets called once the thing "after" the do! finishes.
Whereas Async.Start fires up the workflow on (another) background thread and returns immediately without waiting for it to finish.

FSharp: Using CSV Type Provider Async

I am using the csv type provider to collect some data from a series of files I have on Azure blob storage:
#r "../packages/FSharp.Data.2.0.9/lib/portable-net40+sl5+wp8+win8/FSharp.Data.dll"
open FSharp.Data
type censusDataContext = CsvProvider<"https://portalvhdspgzl51prtcpfj.blob.core.windows.net/censuschicken/AK.TXT">
type stateCodeContext = CsvProvider<"https://portalvhdspgzl51prtcpfj.blob.core.windows.net/censuschicken/states.csv">
let stateCodes = stateCodeContext.Load("https://portalvhdspgzl51prtcpfj.blob.core.windows.net/censuschicken/states.csv");
let fetchStateData (stateCode:string)=
let uri = System.String.Format("https://portalvhdspgzl51prtcpfj.blob.core.windows.net/censuschicken/{0}.TXT",stateCode)
censusDataContext.Load(uri).Rows
let usaData = stateCodes.Rows
|> Seq.collect(fun r -> fetchStateData(r.Abbreviation))
|> Seq.length
I now want to run these async and I am running into a problem with AsyncLoad:
let fetchStateDataAsync(stateCode:string)=
async{
let uri = System.String.Format("https://portalvhdspgzl51prtcpfj.blob.core.windows.net/censuschicken/{0}.TXT",stateCode)
let! stateData = censusDataContext.AsyncLoad(uri)
return stateData.Rows
}
let usaData = stateCodes.Rows
|> Seq.collect(fun r -> fetchStateDataAsync(r.Abbreviation))
|> Seq.length
The error message is
The type 'Async<seq<CsvProvider<...>.Row>>' is not compatible with the type 'seq<'a>'
Forgive my lack of async knowledge, but do I have to use something other than Seq.Collect when applying async functions?
Thanks in advance
The problem is that turning code to asynchronous (by wrapping it in the async { .. } block) changes the result from seq<Row> to Async<seq<Row>> - that is, you now get an asynchronous computation that will eventually complete and return the sequence.
To fix this, you need to somehow start the computation and wait for the result. There is a number of choices - like running one by one sequentially. Probably the easiest option (and maybe the best - depending on what you want to do) is to run the computations in parallel:
let getAll =
stateCodes.Rows
|> Seq.map(fun r -> fetchStateDataAsync(r.Abbreviation))
|> Async.Parallel
This gives you an asynchronous computation that runs all the downloads and returns an array of results. You can run this synchronously (and block) and get the results:
getAll |> Async.RunSynchronously
|> Seq.collect id
|> Seq.length
If you want to run the downloads asynchronously in the background you can do that to, but you need to specify what to do with the result. For example:
async {
let! all = getAll
all |> Seq.collect id |> Seq.length |> printfn "Length %d" }
|> Async.Start

Resources