Share via


從主控台應用程式連接到 Team Foundation Server

您可以用程式設計方式連接到執行 Team Foundation 然後存取該伺服器上的 Team 專案的伺服器,則您可以使用下列範例。 如果您修改這個範例,您可以使用 Getting Additional Team Foundation Services 本主題中稍後說明的服務。 因為 Acting on Behalf of Another User (Impersonation) 本主題中稍後說明,您可以使用模擬,您也可以代表其他動作。

本主題內容

範例

您也可以列出 Team 專案集合及其包含的 Team 專案,如果您使用下列範例。

使用這個範例

  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,請使用 Team Explorer 開啟伺服器上的 Team 專案,並驗證伺服器的 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("http://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 類別時,您可以存取 Team 專案集合的服務。 例如, TfsConfigurationServerITeamFoundationRegistry 服務提供伺服器登錄中的屬性。 從 TfsTeamProjectCollection 保護同一個服務提供 Team 專案集合的已註冊之的屬性。 有些服務僅適用於 Team 專案集合。

服務

TfsConfigurationServer

(伺服器層級)

TfsTeamProjectCollection

(集合層級)

ITeamFoundationRegistry

核取標記

核取標記

IIdentityManagementService

核取標記

核取標記

ITeamFoundationJobService

核取標記

核取標記

IPropertyService

核取標記

核取標記

IEventService

核取標記

核取標記

ISecurityService

核取標記

核取標記

ILocationService

核取標記

核取標記

TswaClientHyperlinkService

核取標記

核取標記

ITeamProjectCollectionService

核取標記

IAdministrationService

核取標記

核取標記

ICatalogService

核取標記

VersionControlServer

核取標記

WorkItemStore

核取標記

IBuildServer

核取標記

ITestManagementService

核取標記

ILinking

核取標記

ICommonStructureService3

核取標記

IServerStatusService

核取標記

IProcessTemplates

核取標記

動作表示另一位使用者 (模擬)

當您連接到 Team Foundation Server時,您可以使用支援模擬表示識別 (Identity) 動作 (而非執行應用程式的方法。 執行的所有作業根據該連接表示模擬的識別來執行。 例如,您的應用程式可在使用者執行的識別,但是建立模擬使用者 B. Team Foundation Server 與的連接。 在這些條件下如果使用者 A 登錄到原始程式碼,變更集記錄使用者 B 簽入變更。

Bb286958.collapse_all(zh-tw,VS.110).gif使用 Team Foundation 識別。

當您連接到 Team Foundation Server 指定識別模擬時,您可以使用 IdentityDescriptor 物件。 IdentityDescriptor 指定 Team Foundation 定義的識別。 當您使用這項策略時,您不需要指定密碼。 驗證的識別 (Identity) 必須具有 [表示其他使用者所做要求] 使用權限,但是有一點例外,就是,當驗證 (使用者 A) 和 (模擬使用者 B) 識別是否相同。

伺服器層級

  • TfsConfigurationServer.TfsConfigurationServer(RegisteredConfigurationServer, IdentityDescriptor)

  • TfsConfigurationServer.TfsConfigurationServer(Uri, IdentityDescriptor)

集合層級

  • TfsTeamProjectCollection.TfsTeamProjectCollection(RegisteredProjectCollection, IdentityDescriptor)

  • TfsTeamProjectCollection.TfsTeamProjectCollection(Uri, IdentityDescriptor)

Bb286958.collapse_all(zh-tw,VS.110).gif使用驗證的認證。

當您連接到 Team Foundation Server 指定識別模擬時,您可以使用 ICredentials 物件。 這個策略不需要特殊的使用權限,但是,您必須能夠取得識別的密碼 ICredentials 建立物件。

當您連接到 Team Foundation Server 處理要求新的認證時,您也可以指定 ICredentialsProvider 的實作。 系統會要求您指定新的認證時, ICredentialsProvider 的實作。 ICredentials 物件指定的認證沒有成功驗證或未授權執行作業。

若要提示使用者,您可以使用 UICredentialsProvider 類別,只會顯示登入對話方塊會提示您輸入新的使用者認證的實作 ICredentialsProvider

伺服器層級

集合層級

Bb286958.collapse_all(zh-tw,VS.110).gif使用技術的組合

當您連接到 Team Foundation Server時,您可以使用 Team Foundation 識別和驗證的認證。 例如,您的應用程式可能會在使用者的認證下,不過,您可以為使用者 B 使用認證並為使用者指定 IdentityDescriptor C,當您連接到 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 專案集合組織您的伺服器

在 Team Foundation Server 中連接 Team 專案

介紹 TfsConnection、TfsConfigurationServer 和 TfsTeamProjectCollection 類別 Microsoft 網站

使用版本控制 API 的 TFS 模擬 Microsoft 網站上。