Can TeamFoundationServer’s GetService ever return null?


You've likely seen a lot of blog posts (mine included) that use the TeamFoundationServer's GetService method to get an instance of a proffered interface/service, for instance VersionControlServer (in Microsoft.TeamFoundation.VersionControl.Client):


    TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(wsInfo.ServerUri.AbsoluteUri);


    VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer)); 



The question came up internally whether it was possible to have it return null - if you're asking for one of our registered interfaces or one of our specifically supported classes (for instance, WorkItemStore, VersionControlServer), then you'll either get the instance or an exception if there's a problem.


However, and this is more trivia than anything useful, if you ask for something outside of that, you'll get null back.  Here's some PowerShell output showing that in action:


PS C:\Documents and Settings\jmanning> [reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")


 


GAC    Version        Location


---    -------        --------


True   v2.0.50727     C:\WINDOWS\assembly\GAC_32\Microsoft.TeamFoundation.Client\8.0.0.0__b03f5f7f11d50a3a\Microsoft.TeamFoundation.Client.dll


 


 


PS C:\Documents and Settings\jmanning> $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer("http://tkbgitvstfat01:8080")


PS C:\Documents and Settings\jmanning> $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) -eq $null


False


PS C:\Documents and Settings\jmanning> $tfs.GetService([System.Collections.ArrayList]) -eq $null


True


PS C:\Documents and Settings\jmanning> $tfs.GetService([System.Collections.IList]) -eq $null


True


 


Here’s an exception getting thrown when we do something like pass null:


 


PS C:\Documents and Settings\jmanning> $tfs.GetService($null) -eq $null


Exception calling "GetService" with "1" argument(s): "Value cannot be null.


Parameter name: serviceType"


At line:1 char:16


+ $tfs.GetService( <<<< $null) -eq $null


PS C:\Documents and Settings\jmanning>


Comments (1)
  1. Rob Caron has put out a bunch of posts over the past week you should check out.

    The VSTE for DB Professionals…

Comments are closed.

Skip to main content