1
votes

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)

4
Change GetRepoTry4(); to Task.Run(async () => { await GetRepoTry4(); }).Wait(); and private static async void GetRepoTry4() to private static async Task GetRepoTry4()whoisj
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 postLike a Fox
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<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
Can you make an answer with example? Please :SLike a Fox

4 Answers

1
votes

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.

0
votes

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.

0
votes

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
votes

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.