C# 用戶端連結庫範例
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
下列範例示範如何使用 .NET 用戶端連結庫擴充及整合 Azure DevOps。
GitHub 中的範例
在 [.NET 範例 GitHub] 頁面上,您可以找到許多範例,其中包含如何執行這些範例的指示。
其他範例
此頁面上的 REST 範例需要下列 NuGet 套件:
- Microsoft.TeamFoundationServer.Client
- Microsoft.VisualStudio.Services.Client
- Microsoft.VisualStudio.Services.InteractiveClient
範例:使用 REST 型 HTTP 用戶端
// https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client/
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
// https://www.nuget.org/packages/Microsoft.VisualStudio.Services.InteractiveClient/
using Microsoft.VisualStudio.Services.Client;
// https://www.nuget.org/packages/Microsoft.VisualStudio.Services.Client/
using Microsoft.VisualStudio.Services.Common;
/// <summary>
/// This sample creates a new work item query for New Bugs, stores it under 'MyQueries', runs the query, and then sends the results to the console.
/// </summary>
public static void SampleREST()
{
// Connection object could be created once per application and we use it to get httpclient objects.
// Httpclients have been reused between callers and threads.
// Their lifetime has been managed by connection (we don't have to dispose them).
// This is more robust then newing up httpclient objects directly.
// Be sure to send in the full collection uri, i.e. http://myserver:8080/tfs/defaultcollection
// We are using default VssCredentials which uses NTLM against an Azure DevOps Server. See additional provided
// Create a connection with PAT for authentication
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
// Create instance of WorkItemTrackingHttpClient using VssConnection
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
// Get 2 levels of query hierarchy items
List<QueryHierarchyItem> queryHierarchyItems = witClient.GetQueriesAsync(teamProjectName, depth: 2).Result;
// Search for 'My Queries' folder
QueryHierarchyItem myQueriesFolder = queryHierarchyItems.FirstOrDefault(qhi => qhi.Name.Equals("My Queries"));
if (myQueriesFolder != null)
{
string queryName = "REST Sample";
// See if our 'REST Sample' query already exists under 'My Queries' folder.
QueryHierarchyItem newBugsQuery = null;
if (myQueriesFolder.Children != null)
{
newBugsQuery = myQueriesFolder.Children.FirstOrDefault(qhi => qhi.Name.Equals(queryName));
}
if (newBugsQuery == null)
{
// if the 'REST Sample' query does not exist, create it.
newBugsQuery = new QueryHierarchyItem()
{
Name = queryName,
Wiql = "SELECT [System.Id],[System.WorkItemType],[System.Title],[System.AssignedTo],[System.State],[System.Tags] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] = 'Bug' AND [System.State] = 'New'",
IsFolder = false
};
newBugsQuery = witClient.CreateQueryAsync(newBugsQuery, teamProjectName, myQueriesFolder.Name).Result;
}
// run the 'REST Sample' query
WorkItemQueryResult result = witClient.QueryByIdAsync(newBugsQuery.Id).Result;
if (result.WorkItems.Any())
{
int skip = 0;
const int batchSize = 100;
IEnumerable<WorkItemReference> workItemRefs;
do
{
workItemRefs = result.WorkItems.Skip(skip).Take(batchSize);
if (workItemRefs.Any())
{
// get details for each work item in the batch
List<WorkItem> workItems = witClient.GetWorkItemsAsync(workItemRefs.Select(wir => wir.Id)).Result;
foreach (WorkItem workItem in workItems)
{
// write work item to console
Console.WriteLine("{0} {1}", workItem.Id, workItem.Fields["System.Title"]);
}
}
skip += batchSize;
}
while (workItemRefs.Count() == batchSize);
}
else
{
Console.WriteLine("No work items were returned from query.");
}
}
}
驗證
若要變更 Azure DevOps 的驗證方法,請在建立 VssCredential 時變更傳遞至 VssConnection 的 VssCredential 類型。
REST 服務的個人存取令牌驗證
public static void PersonalAccessTokenRestSample()
{
// Create instance of VssConnection using Personal Access Token
VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
}
REST 服務的 Visual Studio 登入提示 (Microsoft 帳戶或 Microsoft Entra 後端) (僅限.NET Framework)
由於 .NET Core 版本不支援互動式對話,因此此範例只適用於用戶端的 .NET Framework 版本。
public static void MicrosoftAccountRestSample()
{
// Create instance of VssConnection using Visual Studio sign-in prompt
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssClientCredentials());
}
Microsoft REST 服務的 Entra 驗證
public static void AADRestSample()
{
// Create instance of VssConnection using Azure AD Credentials for Azure AD backed account
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssAadCredential(userName, password));
}
REST 服務的 OAuth 驗證
如需詳細資訊,請參閱 Azure DevOps 驗證範例和 Microsoft 身分識別平台 和 OAuth 2.0 授權碼流程。
public static void OAuthSample()
{
// Create instance of VssConnection using OAuth Access token
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssOAuthAccessTokenCredential(accessToken));
}