Home > Software engineering >  Cannot implicitly convert type 'System.Threading.Tasks.Task
Cannot implicitly convert type 'System.Threading.Tasks.Task

Time:06-04

Trying to run the simplest of Azure Service bus apps:

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

        }
    }
}

But it wont complie:

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)

Im new to C#, but cant find what i need to do to fix this.

I also tried this:

NamespaceProperties props = await client.GetNamespacePropertiesAsync();

CodePudding user response:

You need to use await operator because your method GetNamespacePropertiesAsync is asynchronous, and mark your Main method as async because of you are calling await GetNamespacePropertiesAsync asynchronously and wait till asynchronous operation is completed. Try:

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();
    
        }
    }
}
  • Related