Rajesh Kumar
Rajesh Kumar

Reputation: 70

Null reference exception while getting TFS build details

I am trying to get latest build details from my TFS server but the following line throws a null-reference exception.

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

"An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.TeamFoundation.TestManagement.Client.dll

Additional information: Object reference not set to an instance of an object."

This is the surrounding code

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();
        }
    }
}

Upvotes: 0

Views: 433

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30402

Maybe something wrong with your references. I tested your script, it works with the Microsoft Team Foundation Server Extended Client package installed.

  1. Remove current references
  2. Just try to install the NuGet package Microsoft Team Foundation Server Extended Client by running following command in the Package Manager Console:

PM> Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1

  1. Add below reference based on your script:

using Microsoft.TeamFoundation.Client;

Then try it again.

See below screenshot, it can get the project name correctly. enter image description here

Upvotes: 1

Related Questions