As the famous blog post from Stephen Cleary dictates, one should never try to run async code synchronously (e.g. via Task.RunSynchronously()
or accessing Task.Result
). On the other hand, you can't use async/await inside lock
statement.
My use case is ASP.NET Core app, which uses IMemoryCache
to cache some data. Now when the data is not available, (e.g. cache is dropped) I have to repopulate it, and that should be guarded with lock
.
public TItem Get<TItem>(object key, Func<TItem> factory)
{
if (!_memoryCache.TryGetValue(key, out TItem value))
{
lock (_locker)
{
if (!_memoryCache.TryGetValue(key, out value))
{
value = factory();
Set(key, value);
}
}
}
return value;
}
In this example, the factory function can not be async! What should be done if it has to be async?