簡體   English   中英

方法返回 'System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]' 而不是結果

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

我認為這是一個異步任務被擱置

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

但我不確定為什么會發生並替換另一種方法的輸出。 如果在下面的代碼中Auth()運行成功,那么UserData()運行,為什么會報錯?

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

據我所知,每個異步都有等待

除了這個:

Console.WriteLine(UserData(graphClient));

您永遠不會等待UserData的結果,這是一種async方法。 所以返回的值只是一個Task 等待結果:

Console.WriteLine(await UserData(graphClient));

編輯:經過進一步檢查......你也沒有從UserData返回任何東西。 因此,雖然您需要等待Task的結果,但沒有可顯示的 刪除Console.WriteLine

await UserData(graphClient);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM