GetService() method of the Team Foundation Server object
This post builds off my
Simple Team Foundation Server Object Model Application post. Also, I am currently using Beta3 Refresh of Team Foundation Server. I would also like to thank
Buck Hodges and
James Manning. They have some awesome examples of using the Team Foundation Object model on their blogs.
The TeamFoundationServer object is the base object from which you will utilize the Team Foundation Server Object Model. Using this object, you can access the various sub-components of the object model, such as the different Team Foundation Core Services, the WorkItemStore, or the VersionControlServer object. To do this, you need to use the GetService() method of the TeamFoundationServer object.
The general syntax for the method is:
public object GetService(Type serviceType)
As you can see, the method returns a general object, so you will need to cast the object into the appropriate type. For example, to return a reference to the classification service in Team Foundation Core Services, you would use the following line of code:
ICommonStructureService css = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
The following lists the different options for serviceType:
- Iregistration - Registration service in Team Foundation Core Services
- ICommonStructureService - Classification service in Team Foundation Core Services
- Ilinking - Linking Service in Team Foundation Core Services
- IEventService - Event Service in Team Foundation Core Services
- IGroupSecurityService - User and Groups service. Part of Security Service in Team Foundation Core Services
- IAuthorizationService - Authorization service. Part of the Security Service in Team Foundation Core Services (I think)
- IServerStatusService - API for polling the TFS server status
- IProcessTemplates - API for working with process templates
- WorkItemStore - API for the Team Foundation Server work item components
- VersionControlServer - API for the Team Foundation Server Version Control system
Ok, given this background, let's enhance the application from my previous post. Let's modify our application to display for us all the events that have been registered with the Registration service. This list should show us all the events we can subscribe to.
To do this, we need to add another using statement:
using Microsoft.TeamFoundation.Server;
Next, we need to obtain a reference to the registration service for the Team Foundation Server. Here is the code to retrieve that reference:
IRegistration r = (IRegistration)tfs.GetService(typeof(IRegistration));
Now, we need to look at each RegistrationEntry, and for each entry, look at the EventType:
foreach (RegistrationEntry re in r.GetRegistrationEntries(""))
{
foreach (EventType et in re.EventTypes)
{
Console.Writeline("\n" + "Event Type Name = " + et.Name);
}
}
When you run the application, you end up with output similar to this:
You now have a list of all the events you can subscribe to.
Given this code, you can springboard into working with other aspects of the Team Foundation Core Services, as well as work items and version control, which we will do in future posts.
Here is the full code for our app now:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
namespace TestConsoleApp1
{
class Program
{
static void Main(string[] args)
{
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("http://mstfs:8080");
Console.WriteLine("\n" + "AuthenticatedUserName = " + tfs.AuthenticatedUserName);
Console.WriteLine("\n" + "ClientCacheDirectoryForInstance = " + tfs.ClientCacheDirectoryForInstance);
Console.WriteLine("\n" + "Culture = " + tfs.Culture.ToString());
Console.WriteLine("\n" + "HasAuthenticated = " + tfs.HasAuthenticated.ToString());
Console.WriteLine("\n" + "InstanceId = " + tfs.InstanceId.ToString());
Console.WriteLine("\n" + "Name = " + tfs.Name);
Console.WriteLine("\n" + "SessionId = " + tfs.SessionId.ToString());
Console.WriteLine("\n" + "TimeZone = " + tfs.TimeZone.StandardName);
Console.WriteLine("\n" + "Uri = " + tfs.Uri.ToString());
IRegistration r = (IRegistration)tfs.GetService(typeof(IRegistration));
foreach (RegistrationEntry re in r.GetRegistrationEntries(""))
{
foreach (EventType et in re.EventTypes)
{
Console.Writeline("\n" + "Event Type Name = " + et.Name);
}
}
}
}
}