RegisteredTfsConnections.GetProjectCollection 我得到空异常 [英] RegisteredTfsConnections.GetProjectCollection I get null exception

查看:22
本文介绍了RegisteredTfsConnections.GetProjectCollection 我得到空异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

        }
    }
}

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

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

[1]: RegisteredTfsConnections.GetProjectCollection 在测试时返回 null服务器,但不在开发服务器上 但我收到 TF31002:无法连接到此 Team Foundation Server 错误.不确定要解决什么问题.

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

推荐答案

您想要连接到 团队项目集合 以获取版本控制客户端.最简单的方法是指定集合的​​ URL.例如,如果您想连接到默认的团队项目集合(无聊地命名为 DefaultCollection):

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

这篇关于RegisteredTfsConnections.GetProjectCollection 我得到空异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆