通过C#访问SourceControl时出现“NullReferenceException未处理”错误

时间:2014-02-10 07:22:54

标签: c# .net tfs tfs2010 source-control-explorer

以下是我试图访问TFS服务器SourceControl的代码:

TeamFoundationServer tfs = new TeamFoundationServer("http://test-server:8080/tfs/CollectionName");
VersionControlServer sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

RecursionType recursion = RecursionType.OneLevel;
Item[] items = null;
ItemSet itemSet = sourceControl.GetItems("$/Product/", recursion);
items = itemSet.Items;

但是当我调试代码时,我得到sourceControl的空值。我的TFS服务器路径也是正确的,我没有得到变量sourceControl

中的值

3 个答案:

答案 0 :(得分:1)

找不到您的服务器:

如果无法找到服务,

TeamFoundationServer.GetService方法会返回Null

请参阅:MSDN Documentation

确保程序可以访问该端口(检查防火墙并尝试telnettest-server:8080

此外,正如建议的那样,TeamFoundationServer.EnsureAuthenticated method可以帮助指出URI何时无效。

答案 1 :(得分:0)

不要编写硬编码的Tfs路径,而是尝试使用以下代码来解析所有在线TFS项目。

List<RegisteredProjectCollection> projectCollections = new List<RegisteredProjectCollection>((RegisteredTfsConnections.GetProjectCollections()));

        // filter down to only those collections that are currently on-line
        var onlineCollections =
            from collection in projectCollections
            where collection.Offline == false
            select collection;

        // fail if there are no registered collections that are currently on-line
        if (onlineCollections.Count() < 1)
        {
            Console.Error.WriteLine("Error: There are no on-line registered project collections");
            Environment.Exit(1);
        }

        // find a project collection with at least one team project
        foreach (var registeredProjectCollection in onlineCollections)
        {
            var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(registeredProjectCollection);

            Workspace workspace = null;
            Boolean createdWorkspace = false;
            String newFolder = String.Empty;

            try
            {
                var versionControl = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));

                var teamProjects = new List<TeamProject>(versionControl.GetAllTeamProjects(false));
            //.......
            }
        }

答案 2 :(得分:0)

您尝试以下代码行。这将带您进入对话窗口以选择在线TFS项目。如果内部有任何在线项目,请选择项目并单击“连接”按钮。如果成功,那么您应该获得VersionControlServer else的实例 什么系统响应你是有效的,因为你没有任何在线项目可以连接。

                 TfsTeamProjectCollection _tpc = null;
                 using (var picker = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false))
                 {
                     if (picker.ShowDialog() == DialogResult.OK)
                     {
                        _tpc = picker.SelectedTeamProjectCollection;
                     }

                     if (_tpc == null)
                     {
                         MessageBox.Show("Please select a team project.");
                         return;
                     }

                    var versionControl = (VersionControlServer)_tpc.GetService(typeof(VersionControlServer));