【问题标题】:Use ContinueWith to make async method使用 ContinueWith 制作异步方法
【发布时间】:2014-08-24 15:40:34
【问题描述】:

我想通过 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() 方法上有问题。

错误:无法将类型“Task”隐式转换为“Task”。An 存在明确的对话(您是否缺少演员表?)。

关注代码:

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;

              });

            });
        });
}

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

【问题讨论】:

  • 为什么不使用await 关键字?您的代码看起来会更简单。
  • 我不认为这是避免 async/await 的好理由,而只是为了正确使用它

标签: c# .net asynchronous


【解决方案1】:

我认为问题在于您的第二个 ContinueWith 返回 Task 而不是 Task&lt;bool&gt;。尝试更改代码如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2015-08-20
    • 2016-07-20
    相关资源
    最近更新 更多