使用ContinueWith来创建异步方法

本文关键字:异步方法 创建 ContinueWith 使用 | 更新日期: 2023-09-27 18:19:09

我想通过StackExchange在redis中创建异步方法。Redis follow code:

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()方法的问题。

错误:不能隐式转换类型"任务"到"任务"。一个存在显式对话(您是否缺少cast?)。

跟随代码:

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;
              });
            });
        });
}

我有什么问题?

使用ContinueWith来创建异步方法

我认为问题是您的第二个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