Reputation: 563
I am having trouble with returning a variable in my async method. I am able to get the code to execute but I am not able to get the code to return the email address.
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
}
Upvotes: 5
Views: 8499
Reputation: 32728
RestSharp has built in methods for doing the Task-based Asynchronous Pattern (TAP). This is invoked via RestClient.ExecuteTaskAsync<T>
method. That will give you a response back, and the response.Data
property will have a deserialized version of your generic argument (rootUser in your case).
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;
}
Note that rootUser
is not a good name for a class in C#. Our normal convention is to PascalCase class names, so it should be RootUser.
Upvotes: 5