John John
John John

Reputation: 1

Unable to call async and parallel method inside my asp.net console application

I have an asp.net console application, and inside this console application I want to call an async method in a parallel manner using WhenAll, here is my console application main method:

static void Main(string[] args)
{
    Marketing ipfd = new Marketing();
    try
    {
        using (WebClient wc = new WebClient()) // call the PM API to get the account id
        {
            //code goes here...
        }
    }
    catch (Exception e)
    {
    }
    var tasks = ipfd.companies.Select(c => gettingCustomerInfo(c.properties.website.value)).ToList();
    var results = await Task.WhenAll(tasks);}
}

And here is the method i am calling :-

class Program
{
    static int concurrentrequests = int.Parse(ConfigurationManager.AppSettings["ConcurrentRequests"]);
    SemaphoreSlim throttler = new SemaphoreSlim(initialCount: concurrentrequests);
    int numberofrequests = int.Parse(ConfigurationManager.AppSettings["numberofrequests"].ToString());
    int waitduration = int.Parse(ConfigurationManager.AppSettings["waitdurationmilsc"].ToString());

    private async Task<ScanInfo> gettingCustomerInfo(string website)
    {
        await throttler.WaitAsync();
        ScanInfo si = new ScanInfo();
        var tasks = ipfd.companies.Select(c =>   gettingCustomerInfo(c.properties.website.value)).ToList();
        var results = await Task.WhenAll(tasks);

But i am getting these exceptions:-

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'

An object reference is required for the non-static field, method, or property '***.Program.gettingCustomerInfo(string)'

So can anyone advice on this? Now I know the first exception is regarding that the Main method itself is not async, but if I define the main method to be async then i will get another exception that the program does not contain a Main method that can be called as end point ?

Upvotes: 0

Views: 190

Answers (1)

Steve Land
Steve Land

Reputation: 4862

There are two ways to get around this

Preferred option

Use the newly available async Main support which is available as of C# 7.1 by taking the following steps:

  • Edit your project file to use C# 7.1 (Properties -> Build -> Advanced -> Select C#7.1 as your language version)

  • Change your Main method to the following:

static async Task Main(string[] args) { ... }

Here is an example project demonstrating a working version:

https://github.com/steveland83/AsyncMainConsoleExample

In case it is of interest, here's an informal set of excercises I wrote up to demonstrate a few ways to handle async tasks (and some common basic mistakes): https://github.com/steveland83/async-lab

Option 2

If, for whatever reason you are unable to use the approach above, you can force the async code to run synchronously (be warned, this is almost always considered bad practice).

var aggregateTask = Task.WhenAll(tasks);
aggregateTask.Wait();

Upvotes: 1

Related Questions