Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running c# async method in f# workflow

I am trying to get the below code to work in a F# async workflow, but I am getting the error "Unexpected symbol '}' in expression". I am fairly new to both F# and async in general. What am I missing here.

let someFunction (req : HttpRequestMesssage) a b = 

    // code
    async{
        let! readToProvider =
            req.Content.ReadAsMultipartAsync(provider)
            |> Async.AwaitIAsyncResult 

    } |> Async.RunSynchronously

    req.CreateResponse(HttpStatusCode.OK)
like image 482
user1206480 Avatar asked Dec 20 '22 04:12

user1206480


1 Answers

You're doing it right. You're only getting the error because you're ending your async block with a let! expression. Change it to return!, or do! ... |> Async.Ignore and you'll be good.

Blocks in F# (neither workflows nor regular code blocks) should not end with let.

Of course if all you're really doing in the workflow is that one call, you don't need the workflow block at all (you never really should need to write a block for a single call). Just do

req.Content.ReadAsMultipartAsync provider
  |> Async.AwaitIAsyncResult 
  |> Async.Ignore
  |> Async.RunSynchronously
req.CreateResponse HttpStatusCode.OK

Or for that matter, just use the built in Tasks Wait, which does the same thing as Async.RunSynchronously:

(req.Content.ReadAsMultipartAsync provider).Wait()
like image 102
Dax Fohl Avatar answered Jan 03 '23 04:01

Dax Fohl