Search code examples
asp.net-coreasp.net-core-webapisystem.data.sqlitemicrosoft.data.sqlite

How to deal with SQLite in an asynchronous project


SQLite is not asynchronous, and the exposed Microsoft.Data.Sqlite ADO.net *Async methods are implemented synchronously under the hood.

My question: how would you use SQLite in a asp.net core 5 API project which could benefit from asynchronous code, and also the async/await keywords to coordinate it?

I see two options

  • use the "fake" *Async methods, and lose the benefits of asynchronous code for the code parts that touch the db. If I understand correctly, in .net core blocking a thread will not lead to deadlocks (https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html). If I understand correctly that would act like synchronous code executing on a thread pool thread, with the added overhead of the async/await machinery.
  • write synchronous code only, and lose the benefits for parts of the code

Which one do you think would be better?

Thanks!


Solution

  • I would tend to use the synchronous Async methods, but it's not a strong preference. The only reason I'd prefer Async is just in case either a different database is used in the future, or SQLite gets real asynchronous methods in a future release.

    What you definitely want to avoid is await Task.Run.