从控制台应用程序连接到 Team Foundation Server
如果你使用以下示例,则可以编程方式连接到运行 Team Foundation 的服务器,然后访问该服务器上的团队项目。 如果你修改此示例,则可以使用本主题后面的Getting Additional Team Foundation Services 介绍的服务。 你还可以使用模拟代表他人进行操作,如本主题后面的Acting on Behalf of Another User (Impersonation)所述。
主题内容
示例
如果你使用以下示例,则可以列出团队项目集合以及它们所包含的团队项目。
使用此示例
创建 C# 控制台应用程序。
添加对下列程序集的引用:
将 Program.cs 的内容替换为本主题后面显示的代码。
在该代码中,替换 URL 中用于构造 TfsConfigurationServer 对象的服务器、端口 和 VDir,以便 URL 指代你的服务器。
提示
若要确保使用正确的 URL,请使用 团队资源管理器 打开你的服务器上的团队项目,并验证该服务器的 URL 属性。
using System; using System.Collections.ObjectModel; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.Framework.Common; using Microsoft.TeamFoundation.Framework.Client; namespace TfsApplication { class Program { static void Main(String[] args) { // Connect to Team Foundation Server // Server is the name of the server that is running the application tier for Team Foundation. // Port is the port that Team Foundation uses. The default port is 8080. // VDir is the virtual path to the Team Foundation application. The default path is tfs. Uri tfsUri = (args.Length < 1) ? new Uri("http://Server:Port/VDir") : new Uri(args[0]); TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri); // Get the catalog of team project collections ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren( new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None); // List the team project collections foreach (CatalogNode collectionNode in collectionNodes) { // Use the InstanceId property to get the team project collection Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]); TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId); // Print the name of the team project collection Console.WriteLine("Collection: " + teamProjectCollection.Name); // Get a catalog of team projects for the collection ReadOnlyCollection<CatalogNode> projectNodes = collectionNode.QueryChildren( new[] { CatalogResourceTypes.TeamProject }, false, CatalogQueryOptions.None); // List the team projects in the collection foreach (CatalogNode projectNode in projectNodes) { Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName); } } } } }
Imports System Imports System.Collections.ObjectModel Imports Microsoft.TeamFoundation.Client Imports Microsoft.TeamFoundation.Framework.Common Imports Microsoft.TeamFoundation.Framework.Client Module Module1 Sub Main(ByVal sArgs() As String) ' Connect to the Team Foundation Server ' Server is the name of the server running the application tier for Team Foundation Server ' Port is the port that the Team Foundation Server uses. The default port is 8080. ' VDir is the virtual path to the Team Foundation application. The default value is tfs. Dim tfsUri As Uri If sArgs.Length = 0 Then tfsUri = New Uri("https://Server:8080/tfs") Else tfsUri = New Uri(sArgs(1)) End If Dim configurationServer As New TfsConfigurationServer(tfsUri) configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(tfsUri) ' Get the catalog of team project collections Dim collectionNodes As ReadOnlyCollection(Of CatalogNode) Dim gVar As Guid() = New Guid() {CatalogResourceTypes.ProjectCollection} collectionNodes = configurationServer.CatalogNode.QueryChildren(gVar, False, CatalogQueryOptions.None) ' List the team project collections For Each collectionNode In collectionNodes Dim collectionId As Guid = New Guid(collectionNode.Resource.Properties("InstanceID")) Dim teamProjectCollection As New TfsTeamProjectCollection(tfsUri) teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId) System.Console.WriteLine("Collection:" + teamProjectCollection.Name) ' Get a catalog of team projects for the collection Dim hVar As Guid() = New Guid() {CatalogResourceTypes.TeamProject} Dim projectNodes As ReadOnlyCollection(Of CatalogNode) projectNodes = collectionNode.QueryChildren(hVar, False, CatalogQueryOptions.None) ' List the team projects in the collection For Each projectNode In projectNodes System.Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName) Next Next End Sub End Module
获取额外的 Team Foundation Services
你可以访问额外服务,方法是使用由抽象类 TfsConnection 定义且由 TfsConfigurationServer 和 TfsTeamProjectCollection 实现的 GetService 方法之一。
在你使用 TfsConfigurationServer 类时,可访问用于整个服务器的服务。 在你使用 TfsTeamProjectCollection 类时,可访问用于团队项目集合的服务。 例如,用于 TfsConfigurationServer 的 ITeamFoundationRegistry 服务提供服务器的已注册属性。 从 TfsTeamProjectCollection 获取的相同服务提供团队项目集合的已注册属性。 某些服务仅适用于团队项目集合。
服务 |
TfsConfigurationServer (服务器级) |
TfsTeamProjectCollection (集合级) |
---|---|---|
代表其他用户(模拟)
当你连接到 Team Foundation Server 时,你可以使用支持模拟的方法,以代表运行你应用程序的身份之外的另一个身份进行操作。 任何基于该连接所执行的操作都将代表所模拟的身份执行。 例如,你的应用程序可以以“用户 A”的身份运行,但创建与模拟用户 B 的 Team Foundation Server 的连接。 如果用户 A 在这些条件下签入对源代码的更改,变更集将记录用户 B 已签入该更改。
使用 Team Foundation 身份
当你连接到 Team Foundation Server 以指定要模拟的身份时,可以使用 IdentityDescriptor 对象。 IdentityDescriptor 指定 Team Foundation 所定义的身份。 当你使用此策略时,无需指定密码。 经身份验证的身份必须有**“代表其他用户发出请求”**权限,除非经身份验证的(用户 A)身份和模拟的身份(用户 B)相同。
服务器级 |
---|
集合级 |
---|
使用经身份验证的凭据
当你连接到 Team Foundation Server 以指定要模拟的身份时,可以使用 ICredentials 对象。 此策略不需要特殊权限,但是你必须能够获取该身份的密码以创建 ICredentials 对象。
当你连接到 Team Foundation Server 以处理对新凭据的请求时,你还可以指定 ICredentialsProvider 的实现。 当由 ICredentials 对象指定的凭据未成功进行身份验证或授权以执行该操作时,系统会调用你指定的 ICredentialsProvider 的实现以请求新凭据。
若要提示用户提供凭据,可以使用 UICredentialsProvider 类,此类可通过显示登录对话框以提示用户提供新凭据来实现 ICredentialsProvider。
服务器级 |
---|
集合级 |
---|
使用技术的组合
连接到 Team Foundation Server 时,你可以同时使用 Team Foundation 身份和经身份验证的凭据。 例如,你的应用程序可能在用户 A 的凭据下运行,但是你可能使用用户 B 的凭据并在连接到 Team Foundation Server 时为用户 C 指定 IdentityDescriptor。 在此情况下,使用该连接发出的请求将作为用户 B 进行身份验证,但代表用户 C 执行。 若要使此策略成功,用户 B 必须有**“代表其他用户发出请求”**权限。
服务器级 |
---|
集合级 |
---|
其他资源
在 Team Foundation Server 中连接到团队项目
Microsoft 网站上的 TfsConnection、TfsConfigurationServer 和 TfsTeamProjectCollection 类简介
Microsoft 网站上的将 TFS 模拟与版本控制 API 一起使用。