以编程方式访问TFS帐户以获取项目名称和工作项

本文关键字:工作 项目 方式 编程 访问 TFS 获取 | 更新日期: 2023-09-27 18:12:38

我一直在尝试访问TFS帐户(编程地使用TFS SDK),以便获得诸如项目名称,工作项名称和每个工作项的开发时间等信息。

我无法获得项目或工作项信息,但是我能够使用类TfsTeamProjectCollection验证并访问TFS。

然而,使用类TfsConfigurationServer,我永远无法对服务器进行身份验证。这是一个遗憾,因为我在网上看到的大多数例子都使用这个TfsConfigurationServer类。

允许我访问TFS的代码:

// Connect to Team Foundation Server.
NetworkCredential netCred = new NetworkCredential("myEmail@email.com", "myPassword");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https://tfs.gfi.pt:4430/tfs/gfi/"),tfsCred);
tpc.Authenticate(); // This one works, and when I enter tpc I can see I am correctly signed-in in TFS (I can for instance check my full name)
ITeamProjectCollectionService projCollect = tpc.GetService<ITeamProjectCollectionService>(); //returns null :(
我有两个问题:
  1. TfsConfigurationServer和TfsTeamProjectCollection之间的区别是什么?我知道第一个通向服务器级,第二个通向集合级,但是,什么是服务器级和集合级呢?
  2. 当我完全能够登录时,为什么我无法获得projectCollection(为什么最后一行返回null,当我确信我在TFS上有2个项目时)?

以编程方式访问TFS帐户以获取项目名称和工作项

关于问题#1,您可以在介绍TfsConnection, TfsConfigurationServer和TfsTeamProjectCollection类中找到一些原因。简单地说,你可以有许多用户数据库,例如Collection,由一个TFS实例(即Server)管理。

我刚刚发现问题是什么。我只需要替换最后一行:

ITeamProjectCollectionService projCollect = tpc.GetService<ITeamProjectCollectionService>(); //returns null :(
由:

// Get the catalog of team project collections
ReadOnlyCollection<CatalogNode> collectionNodes = tpc.CatalogNode.QueryChildren(
new [] { CatalogResourceTypes.TeamProject},
false, CatalogQueryOptions.None);
// List the team project collections
foreach (CatalogNode collectionNode in collectionNodes)
{
    Console.WriteLine(collectionNode.Resource.DisplayName);
}