C# istemci kitaplığı örnekleri
Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2013
.NET istemci kitaplıklarını kullanarak Azure DevOps genişletmeyi ve tümleştirin.
GitHub'daki örnekler
.NET Örnek Örneği Sayfamızda bunların nasıl çalıştırıla ilgili yönergelerin yer GitHub vardır.
Diğer örnekler
Bu sayfada yer alan REST örnekleri için aşağıdaki NuGet gerekir:
- Microsoft.TeamFoundationServer.Client
- Microsoft.VisualStudio.Services.Client
- Microsoft.VisualStudio.Services.InteractiveClient
Not
İş Öğesi İzleme (WIT) ve Test İstemcisi OM'leri 2020'de kullanım dışı olacak şekilde zamanlandı. Daha fazla bilgi için bkz. WIT kullanım dışı ve Test İstemcisi OM.
Örnek: REST tabanlı HTTP istemcisi kullanma
// 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
// examples for creating credentials for other types of authentication.
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());
// 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.");
}
}
}
Kimlik Doğrulaması
Kimlik doğrulama yöntemini Azure DevOps Services veya Azure DevOps Server için, oluştururken VssConnection'a geçirilen VssCredential türünü değiştirebilirsiniz.
REST hizmetleri için Kişisel Erişim Belirteci kimlik doğrulaması
public static void PersonalAccessTokenRestSample()
{
// Create instance of VssConnection using Personal Access Token
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssBasicCredential(string.Empty, pat));
}
Visual Studio rest hizmetleri için oturum açma istemini (Microsoft Azure Active Directory veya Azure Active Directory) (yalnızca .NET Framework)
Etkileşimli iletişim kutuları istemcilerin .NET Core sürümü tarafından desteklenemaysa da, bu örnek yalnızca istemcilerin .NET Framework sürümü için geçerlidir.
public static void MicrosoftAccountRestSample()
{
// Create instance of VssConnection using Visual Studio sign-in prompt
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssClientCredentials());
}
Azure Active Directory REST hizmetleri için kimlik doğrulaması
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 hizmetleri için OAuth Kimlik Doğrulaması
OAuth accessToken'ı alma ve yenileme hakkında bilgi edinmek için bu bağlantıyı izleyin: https://github.com/microsoft/azure-devops-auth-samples
public static void OAuthSample()
{
// Create instance of VssConnection using OAuth Access token
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssOAuthAccessTokenCredential(accessToken));
}