gpt4 book ai didi

c# - PubNub 访问导致异步方法

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:36 25 4
gpt4 key购买 nike

我正在编写一个 API,它将授予对 pubnub channel 的访问权限,但需要等待授予访问权限并返回结果。有人可以告诉我如何等待回复吗?

The return type of .Async is void

我调用的授予访问权限的方法如下;

pubnub.Grant()
.Channels(new string[] {
"channel"
})
.Read(true)
.Write(true)
.AuthKeys(new List<string>() { "xyz" }.ToArray())
.TTL(0)
.Async(new PNAccessManagerGrantResultExt((result, status) =>
{
//I need to await this and return the result in my method
}));

Since it does not return a task I cannot await it and my return statement would execute before this even completes.

如有任何建议,我们将不胜感激。

最佳答案

我已经从 PubNub 支持部门收到了解决这个问题的以下解决方案,见下文

var resultTask = new TaskCompletionSource<PNAccessManagerGrantResult>();

pubnub.Grant()
.Channels(new string[] {
"channel"
})
.Read(true)
.Write(true)
.AuthKeys(new List<string>() { "xyz" }.ToArray())
.TTL(0)
.Async(new PNAccessManagerGrantResultExt((result, status) =>
{
resultTask.TrySetResult(result);
}));

var syncResult = resultTask.Task.Result;

关于c# - PubNub 访问导致异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55220490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com