Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缓存任务结果-AsyncLazy不包含GetAwaiter的定义_C#_Caching_Task - Fatal编程技术网

C# 缓存任务结果-AsyncLazy不包含GetAwaiter的定义

C# 缓存任务结果-AsyncLazy不包含GetAwaiter的定义,c#,caching,task,C#,Caching,Task,我正试图按照我遵循的教程将缓存系统放置到位。这个概念似乎很容易理解,但在实现它时,我会收到以下错误消息: “AsyncLazy”不包含“GetAwaiter”的定义,最佳扩展方法重载“AwaitExtensions.GetAwaiter(TaskScheduler)”需要“TaskScheduler”类型的接收器 下面是我的代码: using System; using System.Linq; using System.Runtime.Caching; using System.Threadi

我正试图按照我遵循的教程将缓存系统放置到位。这个概念似乎很容易理解,但在实现它时,我会收到以下错误消息:

“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;
            }
        }
     }
使用系统;
使用System.Linq;
使用System.Runtime.Caching;
使用System.Threading.Tasks;
使用Microsoft.VisualStudio.Threading;
公共类TaskCache:ITaskCache
{
私有内存缓存_cache{get;}=MemoryCache.Default;
private CacheItemPolicy_defaultPolicy{get;}=new CacheItemPolicy();
公共异步任务AddOrGetExisting(字符串键,Func valueFactory)
{
var asyncLazyValue=新的AsyncLazy(valueFactory);
var existingValue=(AsyncLazy)_cache.AddOrGetExisting(key,asyncLazyValue,_defaultPolicy);
if(existingValue!=null)
{
asyncLazyValue=现有值;
}
尝试
{
var result=await asyncLazyValue;//此处出错
//等待的任务已完成。请检查该任务是否仍然是同一版本
//缓存返回(即等待的任务在等待期间未失效)。
if(asyncLazyValue!=\u cache.AddOrGetExisting(key,new AsyncLazy(valueFactory),\u defaultPolicy))
{
//等待的值不再是最近的值。
//通过递归调用获取最近的值。
return wait AddOrGetExisting(key,valueFactory);
}
返回结果;
}
捕获(例外)
{
//给定密钥的任务对象失败,出现异常。请从缓存中删除该任务。
_缓存。删除(键);
//重新抛出要由调用方处理的异常。
投掷;
}
}
}

我并没有发现什么问题,因为我将我的方法声明为async,所以任何潜在客户都会很感激。

似乎您需要对
asyncLazyValue
进行如下调用
var result=wait asyncLazyValue.GetValueAsync()

你能添加用法吗?@GuruStron当然,答案已更新。谢谢,确实如此!