获取TFS构建详细信息时出现空引用异常

时间:2017-06-30 12:54:04

标签: tfs tfsbuild

我正在尝试从我的TFS服务器获取最新的构建详细信息,但以下行引发了一个空引用异常。

ITestManagementTeamProject project = tms.GetTeamProject(proj.Name);
  

“Microsoft.TeamFoundation.TestManagement.Client.dll中发生了'System.NullReferenceException'类型的未处理异常

     

附加信息:对象引用未设置为对象的实例。“

这是周围的代码

using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.Build.WebApi;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
class Program
{
    static void Main(string[] args)
    {
        TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(TfsTeamProjectCollection.GetFullyQualifiedUriForName("http://tfs2013-99-228:8080/tfs/si nhss"));
        tfs.EnsureAuthenticated();
        VersionControlServer vcs = tfs.GetService<VersionControlServer>();
        ITestManagementService tms = tfs.GetService<ITestManagementService>();

        TeamProject[] teamProjects = vcs.GetAllTeamProjects(true);

        IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));

        foreach (TeamProject proj in teamProjects)
        {
            IBuildDefinition[] defs = buildServer.QueryBuildDefinitions(proj.Name);

            System.Console.WriteLine(string.Format("Team Project: {0}", proj.Name));

            foreach (IBuildDefinition def in defs)
            {
                IBuildDetailSpec spec = buildServer.CreateBuildDetailSpec(proj.Name, def.Name);
                spec.MaxBuildsPerDefinition = 10;
                spec.QueryOrder = Microsoft.TeamFoundation.Build.Client.BuildQueryOrder.FinishTimeDescending;
                spec.DefinitionSpec.Name = def.Name;
                spec.Status = Microsoft.TeamFoundation.Build.Client.BuildStatus.All;

                IBuildQueryResult builds = buildServer.QueryBuilds(spec);

                if (builds.Builds.Length > 0)
                {
                    var buildDetail = builds.Builds[0];

                    ITestManagementTeamProject project = tms.GetTeamProject(proj.Name);

                    System.Console.WriteLine(string.Format("   {0} - {1} - {2}", def.Name, buildDetail.Status.ToString(), buildDetail.FinishTime));
                }
            }

            System.Console.WriteLine();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的推荐可能有问题。我测试了您的脚本,它适用于安装的 Microsoft Team Foundation Server扩展客户端包。

  1. 删除当前参考
  2. 只需尝试通过运行以下命令来安装NuGet包Microsoft Team Foundation Server Extended Client     软件包管理器控制台:
  3.   

    PM&GT;安装包Microsoft.TeamFoundationServer.ExtendedClient   -Version 15.112.1

    1. 根据您的脚本添加以下参考:
    2.   

      使用Microsoft.TeamFoundation.Client;

      然后再试一次。

      见下面的截图,它可以正确获取项目名称。 enter image description here