Я пытаюсь установить систему кеширования согласно руководству, которому я следовал. Концепция кажется простой для понимания, но когда дело доходит до ее реализации, я получаю следующее сообщение об ошибке:
'AsyncLazy' не содержит определения для 'GetAwaiter' и перегрузки лучшего метода расширения 'AwaitExtensions.GetAwaiter (TaskScheduler)' требует получателя типа 'TaskScheduler'
Вот мой код:
using System;
using System.Linq;
using System.Runtime.Caching;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
public class TaskCache : ITaskCache
{
private MemoryCache _cache { get; } = MemoryCache.Default;
private CacheItemPolicy _defaultPolicy { get; } = new CacheItemPolicy();
public async Task<T> AddOrGetExisting<T>(string key, Func<Task<T>> valueFactory)
{
var asyncLazyValue = new AsyncLazy<T>(valueFactory);
var existingValue = (AsyncLazy<T>)_cache.AddOrGetExisting(key, asyncLazyValue, _defaultPolicy);
if (existingValue != null)
{
asyncLazyValue = existingValue;
}
try
{
var result = await asyncLazyValue; // ERROR HERE
// The awaited Task has completed. Check that the task still is the same version
// that the cache returns (i.e. the awaited task has not been invalidated during the await).
if (asyncLazyValue != _cache.AddOrGetExisting(key, new AsyncLazy<T>(valueFactory), _defaultPolicy))
{
// The awaited value is no more the most recent one.
// Get the most recent value with a recursive call.
return await AddOrGetExisting(key, valueFactory);
}
return result;
}
catch (Exception)
{
// Task object for the given key failed with exception. Remove the task from the cache.
_cache.Remove(key);
// Re throw the exception to be handled by the caller.
throw;
}
}
}
Я действительно не понимаю, что не так, так как я объявил мой метод как asyn c, поэтому мы будем благодарны за любое руководство.