[英]Use ContinueWith to make async method
我想通過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;
});
});
});
}
我怎么了 以及如何解決此問題?謝謝...
我認為問題在於您的第二個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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.