繁体   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