【问题标题】:Tring to get GitHub repo via Octokit通过 Octokit 获取 GitHub repo
【发布时间】:2015-11-17 23:28:09
【问题描述】:

我正在构建一个简单的工具,用于通过用户提供的链接从在线公共 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)

【问题讨论】:

  • GetRepoTry4(); 更改为Task.Run(async () => { await GetRepoTry4(); }).Wait();private static async void GetRepoTry4() 更改为private static async Task GetRepoTry4()
  • 谢谢,我刚刚将 void 更改为 task 并执行了此 GetRepoTry().Wait();还尝试了像您建议的那样令人讨厌的方法,并且效果都很好:)我现在有一个新问题:S要编辑我的帖子
  • 我的猜测是您的新问题的根源(实际上不知道新问题是什么),是缺少正确使用的 async Task 关键字对。一般来说,所有async方法都需要返回一个TaskTask<T>;所有返回TaskTask<T> 的方法都应该是async。此外,您应该尽快将代码放入调度程序并开始使用await
  • 你能举个例子吗?请:S

标签: c# github-api octokit.net


【解决方案1】:

为 Github 安装Octokit Nuget Package。然后添加下面的函数

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

【讨论】:

    【解决方案2】:

    GetRepoTry4(); 更改为Task.Run(async () =&gt; { await GetRepoTry4(); }).Wait(); 并将private static async void GetRepoTry4() 更改为private static async Task GetRepoTry4()

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

    一般来说,所有async 方法都需要返回TaskTask&lt;T&gt;,所有返回TaskTask&lt;T&gt; 的方法都应该是async。此外,您应该尽快将代码放入调度程序并开始使用 await。

    【讨论】:

      【解决方案3】:

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

      private static async Task GetRepoTry4()
      

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

      【讨论】:

      • await GetRepoTry4() 不起作用,因为 await 必须在异步方法中调用并且 Main 不能异步...我确实像你说的那样将调用从 GetRepoTry4() 更改为 @ 987654327@ 工作:) 谢谢! :D 现在是新问题,我认为它与 API 相关......希望知道 GitHub 的人会看到 :S
      • 我已经修改了我的答案,以展示如何让异步方法在 Main 方法中工作。
      【解决方案4】:

      具有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,这是默认值。

      【讨论】:

      • 你是对的! :) 照你说的做了,现在我得到了 repolist。现在需要获取内容:S 有关于 Octokit 的任何类型的文档吗?还是论坛或社区?不知道从哪里开始 :S 我怎样才能看到 repo 里面有什么? (myrepo 对象只有一些统计信息和属性,而不是内容)
      • 使用 Git 协议获取 Repo 内容,该协议在 Git 中实现。听起来你想编写 Git.exe 脚本。如果是这样,请查看LibGit2Sharp
      猜你喜欢
      • 2019-04-11
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多