我试图通过c#代码访问TFS服务器,我一直在这个问题上运行。 只有当我尝试调试代码但是如果我只是尝试构建并运行它成功时,就会出现问题。
var credentials = new NetworkCredential("username","password");
var projects = new TfsTeamProjectCollection(new Uri("http://xxx-xxxx.com:8080/tfs/xxxxx"), credentials);
projects.EnsureAuthenticated();
if (Directory.Exists(localPath))
{
foreach (var item in Directory.GetFiles(localPath, "*.*", SearchOption.AllDirectories))
{
File.SetAttributes(item, FileAttributes.Normal);
}
Directory.Delete(localPath, true);
}
WorkingFolder[] mapping = serverItem.Select(x => new WorkingFolder(x, Path.Combine(localPath, GetDummyString(x)))).ToArray();
var workspace = versionControl.CreateWorkspace(workspaceName, Environment.UserName, Environment.UserName + " Checkout", mapping);
workspace.Get();
它发生在projects.EnsureAuthenticated();
电话上。我明白了:
System.Net.WebException occurred:
HResult=-2146233079
Message=The remote server returned an error: (401) Unauthorized.
Source=System
StackTrace: at System.Net.HttpWebRequest.GetResponse()
InnerException: null
只有在我尝试调试时才会发生这种情况,如果我对该行发表评论,我会收到一条新的webException:Additional information: The remote server returned an error: (401) Unauthorized.
来自versionControl.CreateWorkspace()
更新 我注意到的是,当它在调试时抛出异常时,如果我尝试继续(多次单击F10),异常消失并且代码成功。
更新2: 在对这个问题进行了一段时间的努力之后,似乎我得到这个错误的原因是因为在创建对象时某些属性没有被初始化。如图所示: Link here
从图中可以看出,未能初始化的是AuthorizedIdentity和CatalogNode。任何想法我怎么能解决它?
答案 0 :(得分:1)
添加域名字符串将解决此问题:
var credentials = new NetworkCredential("Username", "Password", "Domain");
var projects = new TfsTeamProjectCollection(new Uri("http://tfsserver:8080/tfs/teamprojectcollection"), credentials);
projects.EnsureAuthenticated();