C# RegisteredTfsConnections.GetProjectCollection我收到空异常

C# RegisteredTfsConnections.GetProjectCollection我收到空异常,c#,tfs,C#,Tfs,我有一些来自MS的示例代码,用于连接到TFS并返回可以正常工作的集合和项目。但是,当我使用不同的代码连接以获取版本控制数据时,我会得到一个null异常错误 using System; using System.Collections.ObjectModel; using Microsoft.TeamFoundation.Client; using Microsoft.TeamFoundation.Framework.Common; using Microsoft.TeamFoundation.F

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

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

        }
    }
}

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


我尝试了这个方法并得到了错误:TF400 324:Team Foundation服务不从下面的代码中从服务器获得:<代码> var版本控制=(版本控制服务器)TFS。GetService(Type of(VeluoCopyServer));<代码>能否清除本地缓存文件夹<代码>%LOCALAPPDATA%\Microsoft\Team Foundation\5.0\Cache(适用于VS 2013)。我关闭了VS并按照建议清除了缓存,然后重新启动并加载了project,但问题相同。不确定为什么部件连接没有问题,但连接到版本控制的其他代码没有问题。是否在该异常上存在
InnerException
?404表示URL不正确。。。确保正确转义集合名称?如果您的收藏是“我的收藏”,则需要使用
http://mydomain.com:8080/tfs/My%20Collection
。。。您可能无法使用获取集合列表的相同URL,即目录服务的URL。您需要列表中其中一个集合的URL。
    Uri tfsUri2 = new Uri("http://mydomain.com:8080/tfs");
    var projects = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri2);
    var versionControl = (VersionControlServer)projects.GetService(typeof(VersionControlServer));
var tfs = new TfsTeamProjectCollection(new Uri("http://example.com:8080/tfs/DefaultCollection"));
var versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));