Share via


从控制台应用程序连接到 Team Foundation Server

可以通过编程方式连接到运行 Team Foundation 然后访问该服务器的团队项目的服务器,如果使用下面的示例。 如果修改该示例,您可以使用 Getting Additional Team Foundation Services 本主题后面介绍的服务。 ,因为 Acting on Behalf of Another User (Impersonation) 本主题后,描述使用模拟,则可以代表其他还操作。

主题内容

示例

您可以列出团队项目集合,并且该团队项目所包含,如果使用下面的示例。

使用此示例

  1. 创建一个C#控制台应用程序。

  2. 添加对下列程序集的引用:

    备注

    如果Microsoft.TeamFoundation.Client和Microsoft.TeamFoundation.Common不会显示在 引用 对话框的 .NET 选项,请使用 浏览 选项添加程序集。可以找到它们在%ProgramFiles% \ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ ReferenceAssemblies \ v2.0。

  3. 使用后会出现在本主题中的代码替换Program.cs的内容。

  4. 在用于构造TfsConfigurationServer对象的代码,请替换 服务器、 端口和 VDir 在URL中,以便URL引用您的服务器。

    提示

    若要,以确保使用正确的URL,请使用 团队资源管理器 打开位于您的服务器上的团队项目,并验证服务器的URL属性。

    Team Foundation Server 属性: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服务

可以访问附加服务使用抽象类 TfsConnection 定义,并 TfsConfigurationServerTfsTeamProjectCollection 执行的某个GetService方法。

当您使用 TfsConfigurationServer 选件类,可以访问整个服务器的服务。 当您使用 TfsTeamProjectCollection 选件类时,可以访问团队的服务项目集合。 例如, TfsConfigurationServerITeamFoundationRegistry 服务提供服务器上注册的属性。 从 TfsTeamProjectCollection 访问的同一服务提供团队项目集合注册的属性。 这些服务仅适用于团队项目集合。

服务

TfsConfigurationServer

(服务器级别)

TfsTeamProjectCollection

(集合级别)

ITeamFoundationRegistry

选中标记

选中标记

IIdentityManagementService

选中标记

选中标记

ITeamFoundationJobService

选中标记

选中标记

IPropertyService

选中标记

选中标记

IEventService

选中标记

选中标记

ISecurityService

选中标记

选中标记

ILocationService

选中标记

选中标记

TswaClientHyperlinkService

选中标记

选中标记

ITeamProjectCollectionService

选中标记

IAdministrationService

选中标记

选中标记

ICatalogService

选中标记

VersionControlServer

选中标记

WorkItemStore

选中标记

IBuildServer

选中标记

ITestManagementService

选中标记

ILinking

选中标记

ICommonStructureService3

选中标记

IServerStatusService

选中标记

IProcessTemplates

选中标记

代表其他用户(模拟)

当您连接到 Team Foundation Server时,可以使用支持模拟委托标识操作除了一个不运行应用程序的方法。 执行的所有操作根据该连接委托所模拟的标识将执行。 例如,应用程序可以运行在用户下的标识,但创建模拟用户B.与 Team Foundation Server 的连接。 在下列情况下将用户的入到源代码的更改,变更集记录用户B注册了更改。

Bb286958.collapse_all(zh-cn,VS.110).gif使用Team Foundation标识

,当您连接到 Team Foundation Server 指定标识模拟时,可以使用 IdentityDescriptor 对象。 IdentityDescriptor 指定 Team Foundation 定义的标识。 当您使用此方法时,不必指定密码。 该验证的标识必须具有 委托另一个用户发出请求 权限,除此之外,验证的(用户 A)和模拟(用户 B)标识相同。

服务器级别

  • TfsConfigurationServer.TfsConfigurationServer(RegisteredConfigurationServer, IdentityDescriptor)

  • TfsConfigurationServer.TfsConfigurationServer(Uri, IdentityDescriptor)

集合级别

  • TfsTeamProjectCollection.TfsTeamProjectCollection(RegisteredProjectCollection, IdentityDescriptor)

  • TfsTeamProjectCollection.TfsTeamProjectCollection(Uri, IdentityDescriptor)

Bb286958.collapse_all(zh-cn,VS.110).gif使用验证的凭据

,当您连接到 Team Foundation Server 指定标识模拟时,可以使用 ICredentials 对象。 此方法不需要特殊权限,但是,您必须能够获取该标识的密码创建 ICredentials 对象。

,当您连接到 Team Foundation Server 处理为新凭据时,还可以指定 ICredentialsProvider 的实现。 系统调用指定请求的新凭据 ICredentialsProvider 的实现,如果由 ICredentials 对象时指定的凭据未成功验证也不被授权执行操作。

若要提示用户输入凭据,可以使用 UICredentialsProvider 选件类,通过显示登录对话框提示的新凭据的用户实现 ICredentialsProvider

服务器级别

集合级别

Bb286958.collapse_all(zh-cn,VS.110).gif使用技术的组合

,当您连接到 Team Foundation Server时,可以使用 Team Foundation 标识和验证的凭据。 例如,应用程序可能运行在用户的凭据下,但是,可以为用户B使用凭据并为用户C指定 IdentityDescriptor ,当您连接到 Team Foundation Server时。 在这种情况下,将使用该连接的请求验证作为用户B,但委托用户C.执行。 为了使此成功的方法,用户B必须具有 委托另一个用户发出请求 权限。

服务器级别

  • TfsConfigurationServer.TfsConfigurationServer(RegisteredConfigurationServer, ICredentials, ICredentialsProvider, IdentityDescriptor)

  • TfsConfigurationServer.TfsConfigurationServer(Uri, ICredentials, ICredentialsProvider, IdentityDescriptor)

集合级别

  • TfsTeamProjectCollection.TfsTeamProjectCollection(RegisteredProjectCollection, ICredentials, ICredentialsProvider, IdentityDescriptor)

  • TfsTeamProjectCollection.TfsTeamProjectCollection(Uri, ICredentials, ICredentialsProvider, IdentityDescriptor)

其他资源

使用团队项目集合组织服务器

在 Team Foundation Server 中连接团队项目

介绍TfsConnection、TfsConfigurationServer和TfsTeamProjectCollection选件类 Microsoft网站上

使用与版本控制API的TFS模拟 Microsoft网站上。