1

我正在构建简单的工具,用于通过用户提供的链接从在线公共 GitHub 存储库下载 .lua 文件。我开始学习异步方法,所以我想测试一下自己。

这是一个控制台应用程序(目前)。最终目标是在 repo 中获取 .lua 文件并询问用户他想要下载哪些文件,但如果我现在连接到 GH,我会很高兴。

我正在使用 Octokit ( https://github.com/octokit/octokit.net ) GitHub API 集成到 .NET。

这是简化的代码;我删除了一些不重要的东西:

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);
            }
        }
    }
}

问题是 await` 关键字,因为它终止方法并返回。

我仍在学习异步方法,所以可能我把事情搞砸了,但即使是我的 ReSharper 也说得很好。

var以前是换task<T>东西的。它对我来说很好,没有警告也没有错误。

我解决了这个await问题。现在,当我最终连接到 GH 并尝试获取 repo 时,它在对 GH 的两次调用中都抛出了异常(首先通过评论然后第二次调用进行了测试)。e.message是一些巨大的东西。

我将它记录到一个文件中,它看起来像一个 HTML 文档。这是(http://pastebin.com/fxJD1dUb

4

4 回答 4

1

更改GetRepoTry4();Task.Run(async () => { await GetRepoTry4(); }).Wait();和。private static async void GetRepoTry4()_private static async Task GetRepoTry4()

这应该让你至少正确连接到足以开始调试真正的问题。

一般来说,所有async方法都需要返回一个TaskorTask<T>并且所有返回一个Taskor的方法Task<T>应该是async。此外,您应该尽快将代码放入调度程序并开始使用 await。

于 2015-08-25T16:48:43.953 回答
0

为 Github安装Octokit Nuget 包。然后添加以下函数

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);
}
于 2017-11-23T11:17:57.610 回答
0

具有重载的构造函数Uri旨在与 GitHub Enterprise 安装一起使用,例如:

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

如果你只是用它来连接 GitHub,你不需要指定这个:

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

您看到的是 HTML 页面,因为基地址不正确 - 所有与 API 相关的操作都使用api.github.com,这是默认值。

于 2015-08-25T07:30:45.637 回答
-1

由于使用了 async/await,您应该将方法的定义更改为GetRepoTry4以下内容:

private static async Task GetRepoTry4()

编辑:然后在Main方法中这样调用它GetRepoTry4().Wait();。这将使该方法GetRepoTry4()处于等待状态。

于 2015-08-24T15:04:08.787 回答