这是我尝试访问TFS服务器源代码管理的代码段:
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
中的值
您的服务器:
如果找不到服务,TeamFoundationServer.GetService
方法将返回Null
。
请参见: MSDN 文档
确保程序可以访问端口(检查防火墙并尝试telnet
test-server:8080
)
此外,正如建议的那样,注释TeamFoundationServer.EnsureAuthenticated
方法可以帮助指出 URI 何时无效。
不要编写硬编码的 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));
//.......
}
}
请尝试以下代码行。这将转到对话框窗口以选择联机 TFS 项目。如果里面有任何可用的在线项目,请选择该项目并单击"连接"按钮。如果成功,那么你应该得到VersionControlServer的实例,否则无论系统响应您什么,都是有效的,因为您没有任何在线项目要连接。
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));