简体   繁体   English

无法通过 web api 连接到 TFS2015 服务器

[英]Trouble connecting to the TFS2015 server via web api

I have been trying to connect to the our TFS2015 server via web api.我一直在尝试通过 web api 连接到我们的 TFS2015 服务器。 I have not done this before.我以前没有这样做过。 My goal is to get last successful build, get all changesets from him and for each changeset an info from which branch it was forwarded to release branch.我的目标是获得最后一次成功构建,从他那里获取所有变更集,并为每个变更集提供一个信息,它是从哪个分支转发到发布分支的。 I cant connect to it and not sure what is my next step.我无法连接到它,不知道下一步是什么。

This is my code:这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            VssConnection connection = new VssConnection(new Uri("http://srv-tfs2015:8080/tfs/"), new VssCredentials(useDefaultCredentials: true));

            Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient buildKlijent = connection.GetClient<Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient>();

            var lastBuild = buildKlijent.GetLatestBuildAsync("Argosy", "Argosy32New");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

The lastBuild contains an VssResourceNotFountException with a message: lastBuild 包含一个带有消息的 VssResourceNotFountException:

API resource location 54481611-01f4-47f3-998f-160da0f0c229 is not registered on http://srv-tfs2015:8080/tfs/ . API 资源位置 54481611-01f4-47f3-998f-160da0f0c229 未在http://srv-tfs2015:8080/tfs/ 上注册

Build resides within team project Argosy which is a part of ArgosyCollection on the server and it's a XAML build definition.构建驻留在团队项目 Argosy 中,它是服务器上 ArgosyCollection 的一部分,它是一个 XAML 构建定义。 Not sure if that makes any difference.不确定这是否有任何区别。

Can you help me point to the right direction?你能帮我指出正确的方向吗?

The documentation of Get Latest says you have to use rest api version 5.1. 获取最新的文档说你必须使用rest api 5.1版。 But TFS 2015 ( API and TFS version mapping ) supports only 2.4.但是 TFS 2015( API 和 TFS 版本映射)仅支持 2.4。 So you have to use Get a list of build and detect the last build from the resulting list through the GetBuildsAsync method.因此,您必须使用获取构建列表并通过GetBuildsAsync方法从结果列表中检测最后一个构建。

As example to view last 10 builds of build definitions:作为查看构建定义的最后 10 个构建的示例:

private static void ListBuildDefinitions(string TeamProjectName)
{
    List<BuildDefinitionReference> buildDefs = BuildClient.GetDefinitionsAsync(TeamProjectName).Result;

    foreach(BuildDefinitionReference buildDef in buildDefs)
    {
       Console.WriteLine("+================BUILD DEFINITION=======================================================");
       Console.WriteLine(" ID:{0, -9}|NAME:{1, -35}|PATH:{2}", buildDef.Id, buildDef.Name, buildDef.Path);
       Console.WriteLine(" REV:{0, -8}|QUEUE:{1, -34}|QUEUE STATUS:{2}", buildDef.Revision, (buildDef.Queue != null) ? buildDef.Queue.Name : "", buildDef.QueueStatus);

       ListBuilds(TeamProjectName, buildDef);
   }
}

private static void ListBuilds(string TeamProjectName, BuildDefinitionReference buildDef)
{
   List<Build> builds = BuildClient.GetBuildsAsync(TeamProjectName, new List<int> { buildDef.Id }).Result;

   if (builds.Count > 0)
   {
       Console.WriteLine("+====================BUILDS================================================================================");
       Console.WriteLine("+    ID      |        NUMBER        |      STATUS     |     START DATE     |    FINISH DATE     | COMMITS");
       Console.WriteLine("+----------------------------------------------------------------------------------------------------------");

        for (int i = 0; i < builds.Count && i < 10; i++)
        {
            var changes = BuildClient.GetBuildChangesAsync(TeamProjectName, builds[i].Id).Result;
            Console.WriteLine(" {0, -12}|{1, -22}|{2, -17}|{3, -20}|{4, -20}|{5}", builds[i].Id, builds[i].BuildNumber, builds[i].Status,
                (builds[i].StartTime.HasValue) ? builds[i].StartTime.Value.ToString() : "",
                (builds[i].FinishTime.HasValue) ? builds[i].FinishTime.Value.ToString() : "", changes.Count);
        }
    }
    else
        Console.WriteLine("+=======================================================================================");
}

Check available functions for TFS 2015 here: Pre-Version 4.1 REST API documentation在此处检查 TFS 2015 的可用函数: Pre-Version 4.1 REST API 文档

尝试通过添加项目集合名称和@字符来修改Uri,例如:

VssConnection connection = new VssConnection(new Uri(@"http://srv-tfs2015:8080/tfs/DefaultCollection"), myCredentials);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM