【发布时间】:2017-01-09 14:28:55
【问题描述】:
我正在尝试从 TFS2015 构建定义中获取信息。我们有大约 100 个 XAML 格式的构建定义和大约 50 个新的 2015 格式。 该服务器是内部 Team Foundation Server。 (Microsoft Visual Studio 团队基础服务器 版本 15.105.25910.0)
我没有使用其他 API,而是使用此处推荐的 Microsoft.TeamFoundationServer.ExtendedClient:https://blogs.msdn.microsoft.com/buckh/2015/08/10/nuget-packages-for-tfs-and-visual-studio-online-net-client-object-model/。
这是我的代码示例:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Serilog;
namespace TFSExperiment
{
class Program
{
// see https://blogs.msdn.microsoft.com/buckh/2015/08/10/nuget-packages-for-tfs-and-visual-studio-online-net-client-object-model/
//Needs nuget package Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 14.102.0
// to use serilogg: Install-Package Serilog ; Install-Package Serilog.Sinks.RollingFile
static void Main(string[] args)
{
var myLog = new LoggerConfiguration()
.WriteTo.RollingFile("..\\..\\Applog\\mylog-{Date}.log").CreateLogger();
TfsConfigurationServer configurationServer =
TfsConfigurationServerFactory.GetConfigurationServer(new Uri("https://tfs.inhouseserver2015.org/tfs/"));
ReadOnlyCollection<CatalogNode> collectionNodes =
configurationServer.CatalogNode.QueryChildren(new[] {CatalogResourceTypes.ProjectCollection}, false,
CatalogQueryOptions.None);
CatalogNode defultTfsCol = collectionNodes.AsQueryable().Single(c=>c.Resource.DisplayName.Equals("DefaultCollection"));
Console.WriteLine(defultTfsCol.Resource.DisplayName);
TfsTeamProjectCollection tfsProjectCollection =
configurationServer.GetTeamProjectCollection(new Guid(defultTfsCol.Resource.Properties["InstanceId"]));
tfsProjectCollection.Authenticate();
var buildServer = (IBuildServer)tfsProjectCollection.GetService(typeof(IBuildServer));
ReadOnlyCollection<CatalogNode> projectNodes = defultTfsCol.QueryChildren(
new[] { CatalogResourceTypes.TeamProject },
false, CatalogQueryOptions.None);
foreach (var proj in projectNodes)
{
var buildDefinitionList = new List<IBuildDefinition>(buildServer.QueryBuildDefinitions(proj.Resource.DisplayName));
foreach (var buildDef in buildDefinitionList)
{
Console.WriteLine(buildDef.Name);
myLog.Information($"{buildDef.Id} --{buildDef.Name} --{buildDef.BuildServer.BuildServerVersion} ");
}
}
Console.WriteLine(" Hit any key to exit ");
Console.ReadKey();
}
}
}
【问题讨论】: