I am learning F# through building an ASP.NET Core app. I need to implement an interface Microsoft.AspNetCore.Identity.IUserStore<'TUser>; this interface features methods that return Task like:
member this.CreateAsync (user : User, cancellationToken : CancellationToken) =
async {
let connectionString = configuration.GetConnectionString("SocialScoreApp")
let! rowCountResult = user |> createAsync connectionString
match rowCountResult with
| Ok _ -> return IdentityResult.Success
| Error e ->
let identityError = new IdentityError()
identityError.Description <- e
return IdentityResult.Failed(identityError)
} |> Async.StartAsTask
But, according to the documentation, Async.StartAsTask "executes a computation on the thread pool". But this is an I/O bound operation so I don't want any new threads involved. Is there any way to achieve that?
Edit: This is the createAsync code for reference:
let createAsync (connectionString : string) (user:User) =
let sql = "..."
let args = dict [
// ...
]
async {
use connection = new SqlConnection(connectionString)
do! connection.OpenAsync() |> Async.AwaitTask
try
let! rowCount = connection.ExecuteAsync(sql, args) |> Async.AwaitTask
return Ok rowCount
with
| e -> return Error e.Message
}
Async.StartAsTaskdoes seem to start the computation on another thread according to how I interpret the documentation. If I remove|> Async.StartAsTaskI get an error, because the return type isAsync<IdentityResult>instead ofTask<IdentityResult>AsyncandTaskinstances are different regarding when they are run :Asyncis run on demand butTaskis run immediately. So it's not possible to create aTaskwithout the "I/O effect" that bathers you.