gpt4 book ai didi

c# - 如何在 RestSharp 中使用 ExecuteAsync 返回变量

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

我在异步方法中返回变量时遇到问题。我能够获取要执行的代码,但无法获取返回电子邮件地址的代码。

    public async Task<string> GetSignInName (string id)
{

RestClient client = new RestClient("https://graph.windows.net/{tenant}/users");
RestRequest request = new RestRequest($"{id}");
request.AddParameter("api-version", "1.6");
request.AddHeader("Authorization", $"Bearer {token}");
//string emailAddress = await client.ExecuteAsync<rootUser>(request, callback);

var asyncHandler = client.ExecuteAsync<rootUser>(request, response =>
{
CallBack(response.Data.SignInNames);
});

return "test"; //should be a variable
}

最佳答案

RestSharp 内置了执行基于任务的异步模式 (TAP) 的方法。这是通过 RestClient.ExecuteTaskAsync<T> 调用的方法。这会给你一个回应,并且 response.Data属性将具有通用参数的反序列化版本(在您的情况下为 rootUser)。

public async Task<string> GetSignInName (string id)
{
RestClient client = new RestClient("https://graph.windows.net/{tenant}/users");
RestRequest request = new RestRequest($"{id}");
request.AddParameter("api-version", "1.6");
request.AddHeader("Authorization", $"Bearer {token}");
var response = await client.ExecuteTaskAsync<rootUser>(request);

if (response.ErrorException != null)
{
const string message = "Error retrieving response from Windows Graph API. Check inner details for more info.";
var exception = new Exception(message, response.ErrorException);
throw exception;
}

return response.Data.Username;
}

请注意 rootUser不是 C# 中类的好名称。我们的常规约定是 PascalCase 类名,因此它应该是 RootUser。

关于c# - 如何在 RestSharp 中使用 ExecuteAsync 返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54581361/

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