Ryvik
Ryvik

Reputation: 463

Method is returning 'System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]' instead of the result

I think it's an asynchronous task being left off at

GraphServiceClient graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(async (requestMessage) =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
            }));

But I'm unsure of why it's occuring and replacing the output of another method. If in the following code Auth() runs successfully, then UserData() runs, why is it running into the error?

using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Configuration;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Security;
using System.Linq;
using AuthenticationResult = Microsoft.Identity.Client.AuthenticationResult;

namespace SharpZure
{
    public class SharpZure
    {
        public static async Task Main(string[] args)
        {

            var graphClient = await Auth();
            Console.WriteLine(UserData(graphClient));
        }
        public static async Task<GraphServiceClient> Auth()
        {
            string[] scopes = new string[] { "user.read" };
            string token = null;
            IPublicClientApplication app;
            app = PublicClientApplicationBuilder.Create(ConfigurationManager.AppSettings["clientId"]).WithAuthority(AadAuthorityAudience.AzureAdMultipleOrgs).Build();
            AuthenticationResult result = null;
            var accounts = await app.GetAccountsAsync();
            var securePassword = new SecureString();
            foreach (char c in "Pass")        // you should fetch the password
                securePassword.AppendChar(c);  // keystroke by keystroke

            result = await app.AcquireTokenByUsernamePassword(scopes, "[email protected]", securePassword).ExecuteAsync();
            Console.WriteLine("Using provided credentials to gather a token");
            token = result.AccessToken;

            GraphServiceClient graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(async (requestMessage) =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
            }));
            return graphClient;
        }
        static async Task UserData(GraphServiceClient graphClient)
        {
            Console.WriteLine("Display user details");
            var currentUser = await graphClient.Me.Request().GetAsync();
            Console.WriteLine(currentUser.DisplayName);
        }
    }
}

Upvotes: 0

Views: 2668

Answers (1)

David
David

Reputation: 218808

as far as I can tell every async has a wait

Except this one:

Console.WriteLine(UserData(graphClient));

You never await the result of UserData, which is an async method. So the returned value is simply a Task. Await the result:

Console.WriteLine(await UserData(graphClient));

Edit: Upon further inspection... You're also not returning anything from UserData. So while you need to await the result of the Task, there's no value to display. Remove the Console.WriteLine:

await UserData(graphClient);

Upvotes: 3

Related Questions