I am trying to access TFS server via c# code and I keep running in this issue. The issue occurs only when I try to debug the code but if I just try to build and run it succeeds.
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();
It happens is on projects.EnsureAuthenticated();
call. I get:
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
It only occurs when I am trying to debug, IF I comment that line out I get a new webException saying: Additional information: The remote server returned an error: (401) Unauthorized.
from the call to versionControl.CreateWorkspace()
UPDATE: What I noticed is when it throws an exception while debugging, if I try to continue (clicking F10 multiple times) the exception disappears and code succeeds.
UPDATE 2: After struggling with this issue for a while it seems the issue why I am getting this error is because some of the properties do not get initialized when I create an object. As you can see in the picture: Link here
As you can see from the picture what fails to get initialized are AuthorizedIdentity and CatalogNode. Any idea how could I solve it?