40
loading...
This website collects cookies to deliver better user experience
out
argument, essentially somewhere that you’re passing a reference to a variable that the host will send to CosmosDB. Since out
is a C# keyword we need to use the F# equivalent, which is outref<T>
:namespace Demo
type ToDo =
{ Id: string
Description: string }
module CreateToDo =
[<FunctionName("CreateGame")>]
let run
([<QueueTrigger("todoqueueforwrite")>] queueMessage: string),
([<CosmosDB("ToDoItems", "Items", ConnectionStringSetting = "CosmosConnection")>] todo: outref<ToDo>)
(log: ILogger)
=
todo <- { Id = Guid.NewGuid().ToString(); Description = queueMessage }
log.LogInformation "F# Queue trigger function inserted one row"
log.LogInformation (sprintf "Description=%s" queueMessage);
outref<ToDo>
as the second argument of our Function and we use the <-
operator to do assignment to the mutable value (outref
is a mutable reference, similar to let mutable
makes a mutable binding).outref<T>
, as the way async operations work (whether it's Task
or Async
based) means that you can't capture an outref
parameter (nor in C# can you use an out
parameter in an async
function).IAsyncCollector<T>
is for, it gives us an interface which we can push output to from within an async operation.namespace Demo
type ToDo =
{ Id: string
Description: string }
module CreateToDo =
[<FunctionName("CreateGame")>]
let run
([<HttpTrigger(AuthorizationLevel.Function, "post", Route = null)>] req: HttpRequest)
([<CosmosDB("ToDoItems", "Items", ConnectionStringSetting = "CosmosConnection")>] todos: IAsyncCollector<ToDo>)
(log: ILogger)
=
async {
use stream = new StreamReader(req.Body)
let! reqBody = stream.ReadToEndAsync() |> Async.AwaitTask
do!
{ Id = Guid.NewGuid().ToString(); Description = reqBody }
|> todos.AddAsync
|> Async.AwaitIAsyncResult
|> Async.Ignore
return OkResult()
} |> Async.AwaitTask
req.Body
stream and then creating a new record that is passed to the IAsyncCollector.AddAsync
, and since it returns Task
, not Task<T>
, we need to ignore the result.async
block to Task<T>
using Async.AwaitTask
, since the Functions host requires Task<T>
to be returned. You could optimise this code using Ply or TaskBuilder.fs, but I kept it simple for this example.40