0

Using the code below I am trying to use octokit.net to connect to the jquery repository, but the code never seems to reach the print statement. Is there any reason why this is the case, I know the await keyword is causing the delay but the core issue is why I can't connect to the repository. Thanks

       static async void test()
    {

        var client = new GitHubClient(new ProductHeaderValue("OctokitTestsJordan"));

        var repository = await client.Repository.Get("jquery", "jquery");

        Console.WriteLine("aa");

    }
Puck
  • 2,080
  • 4
  • 19
  • 30
user3904388
  • 103
  • 8

2 Answers2

2

You cannot use async/await in Console Apps because Console Apps doesnt support an asynchronous main method. If you want to know more about this issue, Stephen Cleary talk about this in his blog : Async Console Programs.

For your code, it will look like this:

using Nito.AsyncEx; // get it on NuGet

class Program
{
    static void Main(string[] args)
    {
        AsyncContext.Run(() => MainAsync(args));
    }

    static async void MainAsync(string[] args)
    {
        await Test();
    }

    private static async Task Test()
    {
        var client = new GitHubClient(new ProductHeaderValue("OctokitTestsJordan"));

        var repository = await client.Repository.Get("jquery", "jquery");

        Console.WriteLine("aa");
    }
}

By the way, Stephen Cleary is one of the most prolific developer on the async/await topic. I recommend you his blog as a great source of contents around this subject.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
1

Changing the code to this solved the problem, upon finding an obscure stack overflow post I missed on my first bout of research, apparently when using await it needs to be paired with 'Task' if anyone is willing to add more detail to why this is the case, please add a comment for future viewers.

    static void Main(string[] args)
    {
        Task.Run(async () => { await test(); }).Wait();
    }

    private static async Task test()
    {

        var client = new GitHubClient(new ProductHeaderValue("OctokitTestsJordan"));

        var repository = await client.Repository.Get("jquery", "jquery");

        Console.WriteLine("aa");

    }
user3904388
  • 103
  • 8