我有设备(如移动电话,支付终端等),可以要求在数据库中持有的卡列表.为了安全起见,设备需要对自己进行授权,并在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();
}
这些都非常简单,省略了参数等,但这不应该影响问题.
在当前的场景中,我实际上并不需要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()
"Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List>' to 'LanguageExt.Guard'."的行上给出一个编译器错误
按照我得到的previous question分的答案,我试着加ToAsync()
,如下所示……
from cards in GetCards().ToAsync()
...但这给了一个编译器错误"'Task<List>' does not contain a definition for 'ToAsync' and the best extension method overload 'TaskTryExtensions.ToAsync(Try)' requires a receiver of type 'Try'."
我不能把电话打到GetCards()
,因为在这样的查询中,你只能打到第一行.我试着反其道而行之(在判断前获得数据).
var cardList = await(
from cards in await GetCards()
from deviceSerialNumber in GetDeviceSerialNumberFromToken().ToAsync()
from _ in GetDevice(deviceSerialNumber).ToAsync()
select cards
);
...但这给出了编译器错误"Could not find an implementation of the query pattern for source type 'List'. 'SelectMany' not found."
在这一点上,我被困住了.像往常一样,我想象有一个非常简单的答案,但我看不到它.有谁能给点建议吗?
谢谢