Reputation: 21
The following Code works within a console application.
public Boolean Graph_IsMemberOfGroup(string Parm_AzureUserID, string Parm_GroupID) {
Boolean Lcl_ReturnValue = false;
Task<string> Lcl_Response = GraphPriv_IsMemberOfGroup(Parm_AzureUserID, Parm_GroupID);
if (Lcl_Response.Result != null) {
Lcl_ReturnValue = Lcl_Response.Result.Contains(Parm_GroupID);
}//end if
return (Lcl_ReturnValue);
}
private async Task<string> GraphPriv_IsMemberOfGroup(string Parm_AzureUserID, string Parm_GroupID) {
string Lcl_Returnvalue = null;
var Lcl_Uri = "https://graph.windows.net/CleMetroSchools.onmicrosoft.com/users/" + Parm_AzureUserID.Trim() + "/checkMemberGroups?api-version=1.6";
string Lcl_RequestBody = @"{""groupIds"": [""" + Parm_GroupID.Trim() + @"""]}";
Global_HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Global_Token.Result);
HttpResponseMessage Lcl_PostResult = await Global_HttpClient.PostAsync(Lcl_Uri, new StringContent(Lcl_RequestBody, Encoding.UTF8, "application/json"));
if (Lcl_PostResult.Content != null) {
Lcl_Returnvalue = await Lcl_PostResult.Content.ReadAsStringAsync();
}//end if
return Lcl_Returnvalue;
}
The call I use is
if (Graph_IsMemberOfGroup(CurrentUser,Group)){
The problem I am having is that when I Use the same code in a plain (not MVC) ASP.net web application. The system does not wait for GraphPriv_IsMemberOfGroup to completed before it tries to process the if (Lcl_Response.Result != null) {
Anything I have tried so far with waits either will not compile or waits forever. I have been searching for several days and all I have managed to do is travel deeper down the rabbit hole of confusion.
Upvotes: 0
Views: 1001
Reputation: 33094
You're misapplying the async/await model here. You shouldn't be looking for a Task<string>
, you should be looking for string
from an awaited method:
public async Task<Boolean> Graph_IsMemberOfGroup(string Parm_AzureUserID, string Parm_GroupID) {
Boolean Lcl_ReturnValue = false;
string Lcl_Response = await GraphPriv_IsMemberOfGroup(Parm_AzureUserID, Parm_GroupID);
return Lcl_Response.Result.Contains(Parm_GroupID);
}
An async method returns a value wrapped in a Task<>
, The await
keyword tells the code to wait for the method to return and unwrap the response. So if an async method returns Task<string>
then you would call that method using string s = await method()
.
Upvotes: 2