RegisteredTfsConnections.GetProjectCollection I ge

2019-06-02 15:09发布

问题:

I have some sample code from MS to connect to TFS and return the collections and projects which works with no problem. But when I use different code to connect to get the version control data I get a null exception error.

using System;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TfsApplication
{
    class Program
    {
        static void Main(String[] args)
        {

        /* Working Code Base */

            // Connect to Team Foundation Server
            //     Server is the name of the server that is running the application tier for Team Foundation.
            //     Port is the port that Team Foundation uses. The default port is 8080.
            //     VDir is the virtual path to the Team Foundation application. The default path is tfs.
            Uri tfsUri = (args.Length < 1) ?
                new Uri("http://mydomain.com:8080/tfs") : new Uri(args[0]);

            TfsConfigurationServer configurationServer =
                TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

            // Get the catalog of team project collections
            ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
                new[] { CatalogResourceTypes.ProjectCollection },
                false, CatalogQueryOptions.None);


            // List the team project collections
            foreach (CatalogNode collectionNode in collectionNodes)
            {
                // Use the InstanceId property to get the team project collection
                Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
                TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);

                    // Print the name of the team project collection
                    Console.WriteLine("Collection: " + teamProjectCollection.Name);

                    // Get a catalog of team projects for the collection
                    ReadOnlyCollection<CatalogNode> projectNodes = collectionNode.QueryChildren(
                        new[] {CatalogResourceTypes.TeamProject},
                        false, CatalogQueryOptions.None);

                    // List the team projects in the collection
                    foreach (CatalogNode projectNode in projectNodes)
                    {
                        Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName);
                    }
            }
/*Non-Working Code Base*/

            var server = RegisteredTfsConnections.GetProjectCollection(tfsUri);
            var projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(server);
            var versionControl = (VersionControlServer)projects.GetService(typeof(VersionControlServer));
            //var newestDate = DateTime.MinValue;
            //Item newestItem = null;
            var items = versionControl.GetItems("$/MyProject/DEV/*.*");
            Console.ReadKey();
            foreach (var item in items.Items)
            {
                Console.WriteLine(item.ServerItem);
            }

        }
    }
}

I modified the code above to try another method found in this other stackoverflow [article][1].

[1]: RegisteredTfsConnections.GetProjectCollection returns null on test server, but not on dev server But I get an TF31002: Unable to connect to this Team Foundation Server error. Not sure what to troubleshoot on this.

    Uri tfsUri2 = new Uri("http://mydomain.com:8080/tfs");
    var projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri2);
    var versionControl = (VersionControlServer)projects.GetService(typeof(VersionControlServer));

回答1:

You want to connect to a Team Project Collection to get a version control client to it. The easiest way to do this is just specify the URL to the collection. For example, if you want to connect to the default Team Project Collection (boringly named DefaultCollection):

var tfs = new TfsTeamProjectCollection(new Uri("http://example.com:8080/tfs/DefaultCollection"));
var versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));


标签: c# tfs tfs2012