我有可以请求数据库中保存的卡列表的设备(例如移动电话、支付终端等)。为了安全起见,设备需要对自己进行授权,并在 JWT 令牌中提供其序列号。如果其中任何一项不匹配,我们将拒绝该请求。
感谢最近对相关问题的大量帮助,我现在有两种在类似场景中使用的方法......
static Either<int, string> GetDeviceSerialNumberFromToken() {
// Get the JWT token from the request header, and extract the device serial number.
// If the token is invalid or serial number isn't present, we return
// an error code (int). If the data is all OK, we return the serial
// number. For simplicity, the latter is used here...
return "abc123";
}
static async Task<Either<int, Device>> GetDevice(string serialNumber) {
// Check if we have a device with that serial number, and some other checks
// As this involves the database, this method is async. Here we simulate that...
await Task.Delay(10);
return new Device();
}
这些非常简化,省略了参数等,但这不应该影响问题。
在当前场景中,我实际上不需要设备对象,我只需要知道GetDevice
返回它,而不是错误代码。
假设我们通过了这两种方法,然后我想获取卡片列表并将其返回。我有一个足够简单的方法来获取列表......
static async Task<List<Card>> GetCards() {
// As this involves the database, this method is async. Here we simulate that...
await Task.Delay(10);
return new List<Card>();
}
我的目的是以我在其他场景中所做的相同方式将这一切联系在一起......
var cardList = await(
from deviceSerialNumber in GetDeviceSerialNumberFromToken().ToAsync()
from _ in GetDevice(deviceSerialNumber).ToAsync()
from cards in GetCards()
select cards
);
因为这将是一个Either
,我会打电话Match
对此,如果前两种方法之一失败,则返回错误代码,但这部分与此问题无关。
我的问题是,这会在调用的行上产生编译器错误GetCards()
"无法将类型“System.Threading.Tasks.Task”隐式转换为“LanguageExt.Guard”。"
在我得到答案之后上一个问题 https://stackoverflow.com/a/72875493/706346,我尝试添加ToAsync()
如下...
from cards in GetCards().ToAsync()
...但这给出了编译器错误““Task”不包含“ToAsync”的定义,并且最佳扩展方法重载“TaskTryExtensions.ToAsync(Try)”需要“Try”类型的接收器。
"
I can't await
打电话给GetCards()
,因为你只能await
此类查询中的第一行。我尝试以错误的方式进行操作,即在检查之前获取数据......
var cardList = await(
from cards in await GetCards()
from deviceSerialNumber in GetDeviceSerialNumberFromToken().ToAsync()
from _ in GetDevice(deviceSerialNumber).ToAsync()
select cards
);
...但这给出了“编译器错误”找不到源类型“List”的查询模式的实现。未找到“SelectMany”。"
此时我被困住了。像往常一样,我想有一个非常简单的答案,但我看不到。有谁可以提供建议吗?
Thanks