.NET 用戶端程式庫

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

用戶端連結庫適用於建置與 Azure DevOps 整合的 Windows 應用程式和服務的 .NET 開發人員。 用戶端連結庫會與工作項目追蹤、版本控制、組建和其他服務整合。 這些套件會取代傳統的 TFS 用戶端 OM 安裝程式,讓您輕鬆取得及轉散發應用程式或服務所需的連結庫。

提示

使用 .NET 用戶端連結庫範例和 Azure DevOps API 參考,尋找對應的 .NET 用戶端類別和 API 方法。

相依性圖表

相依性圖表顯示 Services.Client、Services.InteractiveClient 和 TeamFoundationServer.Client 之間的關聯性。

功能

  • 可從 NuGet.org 下載,並輕鬆地匯入Visual Studio專案
  • 連結庫已獲授權以在應用程式和服務中轉散發 (檢視授權
  • 存取傳統客戶端物件模型 API 和 REST API

注意

REST 型用戶端僅適用於 Azure DevOps 和 TFS 2015 或更新版本。 如需使用用戶端連結庫擴充和整合 Azure DevOps 的詳細資訊,請參閱 擴充 Team Foundation

套件和 Azure DevOps 版本對應數據表

套件版本 Azure DevOps 版本
16.205.x versions >= Azure DevOps Server 2022
16.170.x versions >= Azure DevOps Server 2020
16.153.x versions >= Azure DevOps Server 2019 Update 1
16.143.x versions >= Azure DevOps Server 2019
15.131.x versions >= TFS 2018 Update 1
15.112.x versions >= TFS 2017 Update 1
14.102.x versions >= TFS 2015 Update 3
14.95.x versions >= TFS 2015 Update 2
14.89.x versions >= TFS 2015 Update 1
14.83.x versions >= TFS 2015

如需最新的預覽版本,請參閱 NuGet 套件庫

REST 套件

套件和描述 二進位
Microsoft.VisualStudio.Services.Client
透過公用 REST API 提供共用平臺服務的存取權,例如組織、配置檔、身分識別、安全性等等。
Microsoft.VisualStudio.Services.WebApi.dll、 、 Microsoft.VisualStudio.Services.Common.dllMicrosoft.TeamFoundation.Common.dll
Microsoft.TeamFoundationServer.Client
透過公用 REST API 提供版本控制、工作專案追蹤、建置等等的存取權。
Microsoft.TeamFoundation.Build2.WebApi.dllMicrosoft.TeamFoundation.Core.WebApi.dllMicrosoft.TeamFoundation.WorkItemTracking.Process.WebApi.dllMicrosoft.TeamFoundation.SourceControl.WebApi.dllMicrosoft.TeamFoundation.TestManagement.WebApi.dll等。
Microsoft.VisualStudio.Services.InteractiveClient
支援使用者需要互動式登錄的應用程式。
Microsoft.VisualStudio.Services.Client.Interactive.dll
Microsoft.VisualStudio.Services.Release.Client
透過公用 REST API 提供發行服務的存取權。
Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.dll
Microsoft.TeamFoundation.DistributedTask.Common.Contracts
提供用來透過公用 REST API 存取分散式工作服務的模型。
Microsoft.TeamFoundation.DistributedTask.Common.Contracts.dll
Microsoft.TeamFoundation.DistributedTask.WebApi
透過公用 REST API 提供分散式工作服務的存取權。
Microsoft.TeamFoundation.DistributedTask.WebApi.dll
Microsoft.VisualStudio.Services.ServiceHooks.WebApi
透過公用 REST API 提供服務攔截服務的存取權。
Microsoft.VisualStudio.Services.ServiceHooks.WebApi.dll
Microsoft.VisualStudio.Services.Gallery.WebApi
透過公用 REST API 提供資源庫服務的存取權。
Microsoft.VisualStudio.Services.Gallery.WebApi.dll
Microsoft.VisualStudio.Services.Notifications.WebApi
透過公用 REST API 提供通知服務的存取權。
Microsoft.VisualStudio.Services.Notifications.WebApi.dll
Microsoft.VisualStudio.Services.ExtensionManagement.WebApi
透過公用 REST API 提供延伸模組管理服務的存取權。
Microsoft.VisualStudio.Services.ExtensionManagement.WebApi.dll
Microsoft.VisualStudio.Services.MemberEntitlementManagement.WebApi
透過公用 REST API 提供成員權利管理服務的存取權。
Microsoft.VisualStudio.Services.MemberEntitlementManagement.WebApi.dll
Microsoft.VisualStudio.Services.ServiceEndpoints.WebApi
透過公用 REST API 提供服務端點的存取權。
Microsoft.VisualStudio.Services.ServiceEndpoints.WebApi.dll
Microsoft.VisualStudio.Services.Search.Client
透過公用 REST API 提供搜尋服務的存取權。
Microsoft.VisualStudio.Services.Search.Shared.WebApi.dll, Microsoft.VisualStudio.Services.Search.WebApi.dll
Microsoft.TeamFoundation.PublishTestResults
此工作可用來發佈測試結果,並在 Azure DevOps 上上傳測試附件。 此套件支援下列結果格式:1。 JUnit - 從 Junit 專案發佈測試,2。 NUnit - 從 Nunit 專案發佈測試,3。 VSTest - 從 Visual Studio 專案發佈測試,4。 Xunit - 從 Xunit 專案發佈測試
Microsoft.TeamFoundation.TestClient.PublishTestResults.dll
Microsoft.VisualStudio.Services.Audit.WebApi
透過公用 REST API 提供稽核服務的存取權。
Microsoft.VisualStudio.Services.Audit.WebApi.dll
Microsoft.Azure.Pipelines.Policy.Client
透過公用 REST API 提供管線核准、檢查和授權的存取權。
Microsoft.Azure.Pipelines.Policy.Client.dll

提示

如果您有使用 TFS 用戶端物件模型的現有 Windows 應用程式或服務,請使用 Microsoft.TeamFoundationServer.ExtendedClient

Soap 套件

套件和描述 二進位 檔
Microsoft.TeamFoundationServer.ExtendedClient
使用和管理用戶端應用程式的版本控制、工作專案及建置和其他資源。 此套件不支援 Net Standard Client OM。 只有當 REST API 不提供您需要的功能時,才使用此套件(例如,在 TFVC 中建立工作區)
Microsoft.TeamFoundation.Build.Client.dllMicrosoft.TeamFoundation.DeleteTeamProject.dllMicrosoft.TeamFoundation.Diff.dllMicrosoft.TeamFoundation.Git.Client.dllMicrosoft.TeamFoundation.SharePointReporting.Integration.dll等。

安裝

從 NuGet 套件管理員命令提示字元安裝:

PM> Install-Package Microsoft.TeamFoundationServer.ExtendedClient

連線

若要建立 Azure DevOps 的已驗證連線,請取得您想要使用之服務的 HttpClient,然後針對該服務呼叫方法。

請參閱下列範例:

using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;

const String c_collectionUri = "https://dev.azure.com/fabrikam";
const String c_projectName = "MyGreatProject";
const String c_repoName = "MyRepo";

Uri orgUrl = new Uri(c_collectionUri);

// Connect to Azure DevOps Services
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));

// Get a GitHttpClient to talk to the Git endpoints
using (GitHttpClient gitClient = connection.GetClient<GitHttpClient>())
{
    // Get data about a specific repository
    var repo = gitClient.GetRepositoryAsync(c_projectName, c_repoName).Result;
}

產生互動式對話框的驗證路徑不適用於 .NET 用戶端連結庫的 .NET Standard 版本。 當您使用 .NET Standard 版本時,請更明確地提供認證進行驗證,如下列範例所示。

using System;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

namespace ConsoleApp1
{
    class Program
    {
        const string collectionUri = "https://dev.azure.com/fabrikam";
        const string projectName = "MyGreatProject";
        const string repoName = "MyRepo";
        const string pat = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

        static void Main(string[] args)
        {
            var creds = new VssBasicCredential(string.Empty, pat);
            
            // Connect to Azure DevOps Services
            var connection = new VssConnection(new Uri(collectionUri), creds);
            
            // Get a GitHttpClient to talk to the Git endpoints
            using var gitClient = connection.GetClient<GitHttpClient>();
            
            // Get data about a specific repository
            var repo = gitClient.GetRepositoryAsync(projectName, repoName).Result;
        }
    }
}

如需更多驗證範例,請參閱 .NET 範例

參考

如需詳細資訊和最新的參考檔,請參閱 .NET API 瀏覽器

範例

查看 .NET 範例頁面上範例,或直接查看 .NET GitHub 存放庫上的範例。

如需 NuGet 套件的詳細資訊,請參閱 Microsoft.VisualStudio.Services.Client

已知問題

在單個線程 Apartment 中使用 Azure DevOps OM 時,不會出現互動式驗證對話框(STA)

如果您的程式代碼是從 單個線程 Apartment (STA) 執行,則不會出現互動式驗證對話方塊。 此問題通常是從 WPF 應用程式發生。 若要解決此問題,您可以將初始化方法變更為異步,並要求驗證,如下列範例所示。

async void InitAzureDevOps()
{
    Uri _uri = new Uri("https://dev.azure.com/MyAccount/");

    var creds = new VssClientCredentials(new WindowsCredential(false),
                                         new VssFederatedCredential(false),
                                         CredentialPromptType.PromptIfNeeded);

    VssConnection vssConnection = new VssConnection(_uri, creds);
    await vssConnection.ConnectAsync();

    ...

}

使用 NetStandard 2.0 版的 Azure DevOps OM

針對 NuGet 套件 16.143.1 版,我們支援 NetStandard 2.0。 這些套件與 Azure DevOps Server 2019 RTW 相互關聯,且與 Azure DevOps 完全相容。

Microsoft.TeamFoundationServer.ExtendedClient 套件沒有 NetStandard 支援

Microsoft.TeamFoundationServer.ExtendedClient 不支援符合 NetStandard 規範的版本。

重要

此套件包含我們較舊的SOAP物件模型,我們將其取代為較新的REST物件模型。 我們不再投資較舊的SOAP物件模型,而且沒有計劃建立它的NetStandard版本。