不初始化tfs versionControlServer

时间:2014-03-31 15:36:05

标签: c# tfs

我有控制台程序,需要从源代码控制下载文件(本地tfs 2012)。但是当我尝试初始化VersionControlServer时,我在调试时看到,这个变量等于null。你能帮我解决这个问题吗?

TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(UrlSite));
VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();
versionControlServer.DownloadFile(serverPath, localFile);//throw exception, because versionControlServer=null;

1 个答案:

答案 0 :(得分:1)

由于用于查询TFS无效的凭据,VersionControlServer可能为空。

检查的一种方法是验证TfsTeamProjectCollection.HasAuthenticated的值。另一种方法是调用TfsTeamProjectCollection.Authenticate()方法。 。即,:

TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(UrlSite));

bool hasAuthenticated = teamProjectCollection.HasAuthenticated;

// Authenticate will throw a WebException if invalid credentials/url submitted.
teamProjectCollection.Authenticate();

VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();
versionControlServer.DownloadFile(serverPath, localFile);//throw exception, because versionControlServer=null;