3

As per this post, the top voted answer suggested that we can use async directly in main. Or I misunderstood it?

Can't specify the 'async' modifier on the 'Main' method of a console app

My main class:

public class Program
{
    public static async Task Main(string[] args)
    {
        ApproveEvents ap = new ApproveEvents();
        List<MyModel> result = new List<MyModel>();
        result = await ap.ApproveAsync();
        if (result.count > 0)
        {
            //do something here
        }

    }
}

And,

public class ApproveEvents
{
    public async Task<List<MyModel>> ApproveAsync()
    {
        //blah blah
    }
}

Visual Studio 2017 is complaining about no Main method for an entry point.

How should I resolve this?

xzk
  • 827
  • 2
  • 18
  • 43
  • 2
    Try pointing your app to the latest .net framework on your project properties. – Hackerman Aug 01 '18 at 06:43
  • 1
    `project -> properties -> build -> Advanced -> Language version`, make sure something is selected that is greater than or equal to 7.1, you made need to fiddle depending on what you have (latest Major release may not work) – TheGeneral Aug 01 '18 at 06:55
  • Note that the [tag:visual-studio] tag description says *"DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio."* Please [edit] your question to remove it. – Richardissimo Aug 02 '18 at 10:57

2 Answers2

6

async Task Main is available in C# 7.1. You can change it in build properties (the default is the latest major version, which is 7.0)

AlexK
  • 561
  • 7
  • 22
2

i'd recommend you looking at this topic to help you, it speaks right into your issue.

it stated:

As I showed above, if you want to await an async operation in the entrypoint method, you need to apply some workaround, because the following entrypoint definition is invalid:

public static async Task Main(string[] args) 
{ 
    await BuildWebHost(args).RunAsync(); 
}

in order to make this work you will need to do the following workaroung:

public static void Main(string[] args)
{     
    BuildWebHost(args).RunAsync().GetAwaiter().GetResult();
}

or calling wait() on the task object itself:

public static void Main(string[] args)
{
    BuildWebHost(args).RunAsync().Wait();
}

there is a list of valid entry points in C# 7.1, this is the up to date list:

public static void Main();
public static int Main();
public static void Main(string[] args);
public static int Main(string[] args);
public static Task Main();
public static Task<int> Main();
public static Task Main(string[] args);
public static Task<int> Main(string[] args);
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 1
    According to the list you posted at the bottom it isn't invalid, can you clarify what you mean here? Note that the `async` keyword doesn't change the signature of the method but instructs the compiler to transform the method body, as such the entrypoint you say is invalid is actually the next-to-last one on your list. – Lasse V. Karlsen Aug 01 '18 at 07:01
  • this is why I told you to read the post I attached, it will all connect :) – Barr J Aug 01 '18 at 07:13
  • 2
    Your answer should still stand on its own legs and right now you're saying "this is invalid" and then follow through with "according to this list it is valid". – Lasse V. Karlsen Aug 01 '18 at 07:22
  • my answers encourages the op to do some research by pointing them to the direction, the point in software is to learn what you did wrong and not to receive the answers on a silver plate. This will give him enough info, let him look at the topic, understand and learn the related issue and continue, this is how you evolve :) – Barr J Aug 01 '18 at 07:30