8

I'm trying to connect to TFS 2010 using TFS SDK, but can't get VersionControlServer service.

var servers = RegisteredTfsConnections.GetConfigurationServers(); // ok

then

var tfs = new TfsConfigurationServer(servers.First().Uri, CredentialCache.DefaultNetworkCredentials);
// or
var tfs = new TfsConfigurationServer(servers.First());

both always returns null:

var vc = (VersionControlServer)tfs.GetService<VersionControlServer>(); // null!

What should I do?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • did you used to get the 'vc' as null ? The reason I am asking is that I am receiving the similar error. – Ram Mehta Feb 11 '14 at 11:36
  • It may depend on version of TFS because some API works only for specific version and you must use another API. What version do you run? – abatishchev Feb 11 '14 at 17:13
  • I have TFS2010, does the solution given below work for you ? – Ram Mehta Feb 12 '14 at 05:35
  • If I remember correctly (it was 3 years ago!) it worked. Otherwise I would not accept it for sure. – abatishchev Feb 12 '14 at 05:36
  • I used the solution provided below, I still get the output of version control as null. – Ram Mehta Feb 13 '14 at 06:24
  • 1
    @RamMehta : the TfsTeamprojectCollection.GetService() stuff will ALSO fail silently (eg: simply return null) when there are imperfections in the app.config - if you leave out the parent node for the nodes within for instance.the rest of .Net all runs fine but the TfsTeamProjectCollection instance demonstrates strange behaviour (including not returning any services). – Aidanapword Jul 29 '15 at 10:00

1 Answers1

19

You don't want the configuration server, you want the project collection. The version control service is scoped to a team project collection. For example:

var projectCollection =
    TfsTeamProjectCollectionFactory.GetTeamProjectCollection(registeredProjectCollection);

var versionControl = projectCollection.GetService<VersionControlServer>();

See also: Connect to a Project Collection

Jim Lamb
  • 25,355
  • 6
  • 42
  • 48
  • Where can I get default collection name? (I know nothing about current TFS, I want to get it from current context) – abatishchev Feb 24 '11 at 03:01
  • Yes, that's right. That will get the collections that are registered with the Team Explorer client on the machine. – Jim Lamb Feb 24 '11 at 12:32
  • @JimLamb I tried doing this same , but I am still getting "versionControl" as Null, any solutions to that ? Any more details do you need to help me ? – Ram Mehta Feb 15 '14 at 11:50
  • @RamMehta what version of TFS (server & client) are you using? Do you have a code sample you can share? – Jim Lamb Feb 15 '14 at 22:02
  • @JimLamb We have a 2010 version of the server and I have been coding in VS2012. Does coding in VS2012 make any difference ? – Ram Mehta Feb 18 '14 at 18:43