2014-03-31 31 views
1

我有控制檯程序,它需要從源代碼控制(本地tfs 2012)下載文件。但是當我嘗試初始化VersionControlServer時,我在調試中看到,這個變量等於null。你能幫我解決這個錯誤嗎?不初始化tfs版本控制服務器

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

回答

1

VersionControlServer可能爲空,因爲用於查詢TFS無效的憑據。

一種檢查方法是驗證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;