I created custom build activity in my TFS build template. In that custom activity I added some code which purpose is to connect to TFS read last build number, create custom number and set new number to TFS.
To connect to TFS I am using following code:
NetworkCredential netCred = new NetworkCredential("username", "password");
TfsTeamProjectCollection server = new TfsTeamProjectCollection(new Uri("https://someproject.visualstudio.com/DefaultCollection")), netCred);
server.Authenticate();
That code works properly when I tried to execute from my local computer in some sample console application. But when I tried to execute that code during TFS build it throw exception.