简体   繁体   中英

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, "User@email.com", 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);
        }
    }
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM