使用ContinueWith生成异步方法

时间:2014-08-22 12:27:15

标签: c# .net asynchronous

我想通过StackExchange.Redis在redis中创建异步方法,遵循代码:

public bool Insert<T>(T entity) where T : IBaseEntity
{
    long entityCounter = _redisClient.StringIncrement(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));

    if (entity.Id == 0)
    {
        entity.Id = ((int)GetLastId<T>()) + 1;
    }
    _redisClient.StringSet(CacheProcessPatterns.MakeLastId(entity.GetType().Name), entity.Id);
    string itemRedisKey = CacheProcessPatterns.MakeItemById(entity.GetType().Name, entity.Id);
    bool setStatus = _redisClient.StringSet(itemRedisKey, JsonSerializer.SerializeToString<T>(entity));
    if (setStatus)
    {
        _redisClient.StringSet(CacheProcessPatterns.MakeIdByKey(entity.GetType().Name, entity.Id), entity.Key.ToString());
        _redisClient.StringSet(CacheProcessPatterns.MakeKeyById(entity.GetType().Name, entity.Key.ToString()), entity.Id);
        _redisClient.SetAdd(CacheProcessPatterns.MakeItemKeysByType(entity.GetType().Name), entity.Id);
    }
    else
    {
        entityCounter = _redisClient.StringDecrement(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));
    }
    return setStatus;

}

和其他我尝试做异步但我在第二个ContinueWith()方法上遇到问题。

  

错误:无法将'任务'类型转换为'任务'   存在明确的对话(你错过了演员吗?)。

关注代码:

public Task<bool> Insert<T>(T entity) where T : IBaseEntity
{
   return _redisClient.StringIncrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name))
        .ContinueWith(entityCounter =>
        {

            if (entity.Id == 0)
            {
                entity.Id = ((int)GetLastId<T>().Result);
            }

        }).ContinueWith(_ =>
        {


            _redisClient.StringSetAsync(CacheProcessPatterns.MakeLastId(entity.GetType().Name), entity.Id).ContinueWith(status =>
            {

                string itemRedisKey = CacheProcessPatterns.MakeItemById(entity.GetType().Name, entity.Id);
              _redisClient.StringSetAsync(itemRedisKey, JsonSerializer.SerializeToString<T>(entity)).ContinueWith( setStatus => 
              {
                    if (setStatus.Result)
                    {
                        ITransaction tran = _redisClient.CreateTransaction();
                        tran.StringSetAsync(CacheProcessPatterns.MakeIdByKey(entity.GetType().Name, entity.Id), entity.Key.ToString());
                        tran.StringSetAsync(CacheProcessPatterns.MakeKeyById(entity.GetType().Name, entity.Key.ToString()), entity.Id);
                        tran.SetAddAsync(CacheProcessPatterns.MakeItemKeysByType(entity.GetType().Name), entity.Id);
                        return tran.ExecuteAsync();
                    }
                    else
                    {
                        _redisClient.StringDecrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));
                    }

                    return setStatus;

              });

            });
        });
}

我的问题是什么?以及如何解决这个问题?谢谢...

1 个答案:

答案 0 :(得分:0)

我认为问题在于您的第二个ContinueWith会返回Task而不是Task<bool>。尝试更改代码,如下所示:

public Task<bool> Insert<T>(T entity) where T : IBaseEntity
{
   return _redisClient.StringIncrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name))
        .ContinueWith(entityCounter =>
        {

            if (entity.Id == 0)
            {
                entity.Id = ((int)GetLastId<T>().Result);
            }

        })
        // Explicitly specify task type to be bool
        .ContinueWith<bool>(_ =>
        {
            _redisClient.StringSetAsync(CacheProcessPatterns.MakeLastId(entity.GetType().Name), entity.Id).ContinueWith(status =>
            {

                string itemRedisKey = CacheProcessPatterns.MakeItemById(entity.GetType().Name, entity.Id);
              _redisClient.StringSetAsync(itemRedisKey, JsonSerializer.SerializeToString<T>(entity)).ContinueWith( setStatus =>
              {
                    if (setStatus.Result)
                    {
                        ITransaction tran = _redisClient.CreateTransaction();
                        tran.StringSetAsync(CacheProcessPatterns.MakeIdByKey(entity.GetType().Name, entity.Id), entity.Key.ToString());
                        tran.StringSetAsync(CacheProcessPatterns.MakeKeyById(entity.GetType().Name, entity.Key.ToString()), entity.Id);
                        tran.SetAddAsync(CacheProcessPatterns.MakeItemKeysByType(entity.GetType().Name), entity.Id);
                        return tran.ExecuteAsync();
                    }
                    else
                    {
                        _redisClient.StringDecrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));
                    }

                    return setStatus;

              });

            });

            return true;  // since this is a Task<bool> we need a bool return value