Chr*_*ris 5 .net c# asynchronous restsharp
我在异步方法中返回变量时遇到问题。我能够获取要执行的代码,但无法获取返回电子邮件地址的代码。
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
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,rootUser对于 C# 中的类来说,这不是一个好名称。我们的正常约定是 PascalCase 类名,所以它应该是 RootUser。
| 归档时间: |
|
| 查看次数: |
7943 次 |
| 最近记录: |