1

I'm building simple tool for downloading .lua files from online public GitHub repos via link given by user. I started learning async methods so I wanted to test myself.

It's a console application (for now). The ultimate goal is to get .lua files in a repo and ask the user which ones he wants downloaded, but I'll be happy if I connect to GH for now.

I'm using Octokit (https://github.com/octokit/octokit.net) GitHub API integration to .NET.

This is the reduced code; I removed some of unimportant stuff:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Octokit;

namespace GetThemLuas
{
    class Program
    {
        static readonly GitHubClient Github = new GitHubClient(new ProductHeaderValue ("Testing123"), new Uri("https://www.github.com/"));


        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to GitHub repo downloader");
            GetRepoTry4();

        }

        private static async void GetRepoTry4()
        {
            try
            {
                Console.WriteLine("Searching for data"); //returns here... code below is never ran
                var searchResults = await Github.Search.SearchRepo(new SearchRepositoriesRequest("octokit"));
                if (searchResults != null)
                    foreach (var result in searchResults.Items)
                        Console.WriteLine(result.FullName);
                Console.WriteLine("Fetching data...."); //testing search
                var myrepo = await Github.Repository.Get("Haacked", "octokit.net");
                Console.WriteLine("Done! :)");
                Console.WriteLine("Repo loaded successfully!");
                Console.WriteLine("Repo owner: " + myrepo.Owner);
                Console.WriteLine("Repo ID: " + myrepo.Id);
                Console.WriteLine("Repo Date: " + myrepo.CreatedAt);
            }
            catch (Exception e)
            {
                Console.WriteLine("Ayyyy... troubles"); //never trigged
                Console.WriteLine(e.Message);
            }
        }
    }
}

The problem is the await` keyword as it terminates the method and returns.

I'm still learning async methods so it's possible I messed something up, but even my ReSharper says it fine.

I used var to replace task<T> stuff. It seams OK to me plus no warnings nor errors.

I fixed the await issue. Now when I finally connected to GH and tried to get the repo it threw an exeption at both calls to GH (tested with commenting first then second call). e.message was some huge stuff.

I logged it into a file and it looks like an HTML document. Here it is (http://pastebin.com/fxJD1dUb)

Servy
  • 202,030
  • 26
  • 332
  • 449
Like a Fox
  • 49
  • 1
  • 6
  • Change `GetRepoTry4();` to `Task.Run(async () => { await GetRepoTry4(); }).Wait();` and `private static async void GetRepoTry4()` to `private static async Task GetRepoTry4()` – whoisj Aug 24 '15 at 16:05
  • Thank you, I just changed void to task and did this GetRepoTry().Wait(); Also tried annoymous method like you suggested and it both worked fine :) I have a new problem now doe :S Gonna edit my post – Like a Fox Aug 24 '15 at 18:48
  • My guess is that root of you new problem (without actually know what the new problem is), is the lack of the `async Task` keyword pair being used correctly. Generally speaking all `async` methods need to return a `Task` or `Task`; and all methods that return a `Task` or `Task` should be `async`. Additionally, you should get your code into the dispatcher as quickly as possible and start using `await`. – whoisj Aug 24 '15 at 20:05
  • Can you make an answer with example? Please :S – Like a Fox Aug 24 '15 at 20:23

4 Answers4

1

Change GetRepoTry4(); to Task.Run(async () => { await GetRepoTry4(); }).Wait(); and private static async void GetRepoTry4() to private static async Task GetRepoTry4().

This should get you at least wired up correctly enough to start debugging the real issue.

Generally speaking all async methods need to return a Task or Task<T> and all methods that return a Task or Task<T> should be async. Additionally, you should get your code into the dispatcher as quickly as possible and start using await.

whoisj
  • 398
  • 1
  • 2
  • 10
0

The constructor with the Uri overload is intended for use with GitHub Enterprise installations, e.g:

static readonly GitHubClient Github = new GitHubClient(new ProductHeaderValue ("Testing123"), new Uri("https://github.enterprise.com/"));

If you're just using it to connect to GitHub, you don't need to specify this:

static readonly GitHubClient Github = new GitHubClient(new ProductHeaderValue ("Testing123"));

You're seeing a HTML page because the base address is incorrect - all of the API-related operations use api.github.com, which is the default.

Brendan Forster
  • 2,548
  • 1
  • 24
  • 32
  • You are right! :) Did as you said and now I got the repolist. Need to get the content now :S Is there any kind of documentation on Octokit? Or forum or community? Have no idea where to start :S How can I see what's inside the repo? (myrepo object just has some stats and properties, not contents) – Like a Fox Aug 25 '15 at 09:52
  • Repo content is fetched using Git protocol, which is implemented in Git. Sounds like you want to script Git.exe. If so, take a look at [LibGit2Sharp](https://github.com/libgit2/libgit2sharp). – whoisj Aug 25 '15 at 20:43
0

Install Octokit Nuget Package for Github.Then add below function

public JsonResult GetRepositoryDeatil(long id)
{
    var client = new GitHubClient(new ProductHeaderValue("demo"));
    var tokenAuth = new Credentials("xxxxxxx"); // NOTE: not real token
    client.Credentials = tokenAuth;
    var content = client.Repository.Content.GetAllContents(id).Result;
    List<RepositoryContent> objRepositoryContentList = content.ToList();

    return Json(objRepositoryContentList, JsonRequestBehavior.AllowGet);
}
-1

Due to the use of the async/await you should change the definition of the method GetRepoTry4 to the following:

private static async Task GetRepoTry4()

EDIT: Then in the Main method call it like so GetRepoTry4().Wait();. This will enable the method GetRepoTry4() to be awaited.

Marc Harry
  • 2,410
  • 19
  • 21
  • await GetRepoTry4() didn't work since await must be called in async method and Main can't be made async... I did like you said and changed the call from `GetRepoTry4()` to `GetRepoTry4().Wait` Worked :) Thank you! :D New issue now, I think's it's API related... hopefully someone who knows GitHub will see :S – Like a Fox Aug 24 '15 at 18:52
  • I have amended my answer to show how to get the async method to work in the `Main` method. – Marc Harry Aug 25 '15 at 07:50