TfsConfigurationServer.GetService<VersionControlServer>() always returns null
Asked Answered
S

1

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?

Sloshy answered 23/2, 2011 at 23:51 Comment(6)
did you used to get the 'vc' as null ? The reason I am asking is that I am receiving the similar error.Furlana
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?Sloshy
I have TFS2010, does the solution given below work for you ?Furlana
If I remember correctly (it was 3 years ago!) it worked. Otherwise I would not accept it for sure.Sloshy
I used the solution provided below, I still get the output of version control as null.Furlana
@RamMehta : the TfsTeamprojectCollection.GetService<T>() stuff will ALSO fail silently (eg: simply return null) when there are imperfections in the app.config - if you leave out the <appSettings/> parent node for the <add/> nodes within <configuration/> for instance.the rest of .Net all runs fine but the TfsTeamProjectCollection instance demonstrates strange behaviour (including not returning any services).Known
H
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

Hoplite answered 24/2, 2011 at 2:48 Comment(5)
Where can I get default collection name? (I know nothing about current TFS, I want to get it from current context)Sloshy
Yes, that's right. That will get the collections that are registered with the Team Explorer client on the machine.Hoplite
@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 ?Furlana
@RamMehta what version of TFS (server & client) are you using? Do you have a code sample you can share?Hoplite
@JimLamb We have a 2010 version of the server and I have been coding in VS2012. Does coding in VS2012 make any difference ?Furlana

© 2022 - 2024 — McMap. All rights reserved.