无法隐式转换类型 'System.Threading.Tasks.Task

Cannot implicitly convert type 'System.Threading.Tasks.Task

尝试运行最简单的 Azure 服务总线应用程序:

using System;
using Azure.Messaging.ServiceBus.Administration;

namespace helloapp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            string connectionString = "<connection_string>";
            ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString);
            NamespaceProperties props = client.GetNamespacePropertiesAsync();

        }
    }
}

但它不符合:

Error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task<Azure.Response<Azure.Messaging.ServiceBus.Administration.NamespaceProperties>>' to 'Azure.Messaging.ServiceBus.Administration.NamespaceProperties' (CS0029) (helloapp)

我是 C# 的新手,但找不到解决此问题所需的方法。

我也试过这个:

NamespaceProperties props = await client.GetNamespacePropertiesAsync();

您需要使用 await 运算符,因为您的方法 GetNamespacePropertiesAsync 是异步的,并且将您的 Main 方法标记为 async 因为您正在调用 await GetNamespacePropertiesAsync 异步等待异步操作完成。尝试:

using System;
using Azure.Messaging.ServiceBus.Administration;


namespace helloapp
{
    class Program
    {
        // mark this method as 'async'
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
    
            string connectionString = "<connection_string>";
            ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString);
            NamespaceProperties props = await client.GetNamespacePropertiesAsync();
    
        }
    }
}