RegisteredTfsConnections.GetProjectCollection我得到null异常

时间:2014-07-03 14:09:01

标签: c# tfs tfs2012

我有一些来自MS的示例代码连接到TFS并返回没有问题的集合和项目。但是当我使用不同的代码连接来获取版本控制数据时,我得到一个空的异常错误。

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

        }
    }
}

我修改了上面的代码,尝试在另一个stackoverflow [文章] [1]中找到的另一个方法。

[1]:RegisteredTfsConnections.GetProjectCollection returns null on test server, but not on dev server但是我得到了TF31002:无法连接到此Team Foundation Server错误。不知道该解决什么问题。

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

1 个答案:

答案 0 :(得分:3)

您希望连接到团队项目集合以获取版本控制客户端。最简单的方法是指定集合的​​URL。例如,如果要连接到默认的Team Project Collection(名为DefaultCollection):

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