John John
John John

Reputation: 1

Error on my console application "Program does not contain a static 'Main' method suitable for an entry point"

I want to run Parallel processing inside my asp.net console application. i am using c# 7.1 + Visual Studio 2017

Here is my code:-

 class Program
    {
        static int concurrentrequests = int.Parse(ConfigurationManager.AppSettings["ConcurrentRequests"]);
        static   SemaphoreSlim throttler = new SemaphoreSlim(initialCount: concurrentrequests);
        static int numberofrequests = int.Parse(ConfigurationManager.AppSettings["numberofrequests"].ToString());
        static  int waitduration = int.Parse(ConfigurationManager.AppSettings["waitdurationmilsc"].ToString());
        private static async Task<ScanInfo> gettingCustomerInfo(string website)
        {
            await throttler.WaitAsync();
            ScanInfo si = new ScanInfo();
            int counter = 1;
            try
            {
                //code goes here..
                Console.WriteLine(website + "    -->     " + si.EmailGateway + "    -->     " + si.Status);

            }
            catch (Exception e)
            {

            }
                try
                {
                    using (WebClient wc = new WebClient()) // call the PM API to get the account id 
                    {
                        //code goes here..

                    }
                }
                catch (Exception e)
                {



                }
                //string pmtoken = tokenGet();

                finally
                {

                    throttler.Release();
                }

            }
            return si;
        }

        static async Task Main(string[] args)
        {
            Marketing ipfd = new Marketing();
            try
            {
                using (WebClient wc = new WebClient()) 
                {
                    //code goes here..

                }
            }
            catch (Exception e)
            {


            }
            var tasks = ipfd.companies.Select(c => gettingCustomerInfo(c.properties.website.value)).ToList();
            var results = await Task.WhenAll(tasks);
    }}

now in my above code, i am trying to call the gettingcustomerinfo in parallel manner. but my above code will raise the following error:-

"Program does not contain a static 'Main' method suitable for an entry point"

now if i use :-

static void Main(string[] args)

instead of :-

static async Task Main(string[] args)

i will get this error instead:-

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'.

on:-

var results = await Task.WhenAll(tasks);

so can anyone advice on this please?

Upvotes: 0

Views: 1895

Answers (3)

OKEEngine
OKEEngine

Reputation: 908

I have been doing something like this to work around with the issue for a while.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            AsyncTask().Wait();

        }
        catch (Exception e)
        {
            ...
        }

    }

    static async Task AsyncTask()
    {
        ...
        var httpClient = new HttpClient();     
        var response = await httpClient.PostAsync(queryString, content);
        ...
    }
}

Upvotes: 1

Jon Vote
Jon Vote

Reputation: 663

Try something like this:

public class SomeClass
{
    public async void RunTask()
    {
        await SomeTask();
    }

    private Task<int> SomeTask()
    {
        var x = 10;
        System.Threading.Thread.Sleep(1000);
        return Task.FromResult(x);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var someClass = new SomeClass();
        someClass.RunTask();
    }
}

Upvotes: 0

lindexi
lindexi

Reputation: 4327

Do you use C# 7.2 in your VisualStudio proj?

You must use C# 7.2 or latest version to build the async main.

Try to right click your project and click the property button. And then you should select the build tab and click the advanced button to select the C# 7.2.

enter image description here

enter image description here

The other way is editing your csproj file. You can add <LangVersion>latest</LangVersion> in the property group.

See https://blogs.msdn.microsoft.com/benwilli/2017/12/08/async-main-is-available-but-hidden/

The Chinese version is https://lindexi.gitee.io/post/VisualStudio-%E4%BD%BF%E7%94%A8%E4%B8%89%E4%B8%AA%E6%96%B9%E6%B3%95%E5%90%AF%E5%8A%A8%E6%9C%80%E6%96%B0-C-%E5%8A%9F%E8%83%BD.html

Or you should select the startup main.

enter image description here

Upvotes: 4

Related Questions