There are numerous examples around that explain how to receive TFS events using an ASMX web service. In this post we’ll look at how you can create a WCF service, hosted in IIS or self-hosted, to receive these events.
Prerequisites
- Visual Studio 2008 with Service Pack 1
- Team Foundation Server 2008
Create a WCF Service
The first step is of course to create a WCF service project. In Visual Studio 2008 create a new WCF Service Library using the language of your choice but in this example we’ll use Visual Basic .NET. If you do not see the WCF option select either .NET Framework 3.0 or .NET Framework 3.5 from the dropdown list in the top right-hand corner of the New Project dialog.

This will create a project containing an app.config (which stores the WCF configuration), IService1.vb (which stores the service’s interface), and Service1.vb (which stores the service’s implementation). The new project should look like this:
Renaming the Service Interface and Service Implementation
IService1 and Service1 aren’t the most descriptive names, so we’ll rename them to TfsEventSubscriber and remove the sample service implementation. To do this:
- Rename the file IService1.vb to ITfsEventSubscriber.vb.
- Open the file ITfsEventSubscriber.vb and rename the interface from IService1 to ITfsEventSubscriber.
- Remove the GetData and GetDataUsingContract functions.
- Remove the CompositeType class.
- Open the file app.config and replace any references to IService1 with ITfsEventSubscriber.
- Rename the file Service1.vb to TfsEventSubscriber.
- Open the file TfsEventSubscriber.vb and rename the class from Service1 to TfsEventSubscriber.
- Change the interface the class inherits from IService1 to ITfsEventSubscriber.
- Remove the GetData and GetDataUsingContract functions.
- Open the file app.config and replace any references to IService1 with ITfsEventSubscriber.
- Save all of the files and project and build the project to make sure we haven’t broken anything.
The resulting project should look like this:
Define the Method to Receive TFS Events
The next step is to define a method that will receive the TFS Events. The name of this method is up to you but the convention is to call it Notify and it must accept two string parameters called eventXml and tfsIdentityXml. The eventXml argument will contain the XML about the event occur and this will conform to the schema for that particular event. The tfsIdentityXml argument will contain XML identifying the application tier that raised the event.
We now add this method to the ITfsEventSubscriber and apply the OperationContract attribute. We also need to specify the namespace and action named used by Team Foundation Server when calling this operation. This results in the interface looking like this:
<ServiceContract(Namespace:="http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")> _
Public Interface ITfsEventSubscriber
<OperationContract(Action:="http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify")> _
Sub Notify(ByVal eventXml As String, ByVal tfsIdentityXml As String)End Interface
For the project to compile we must implement this new method in the TfsEventSubscriber which would then look like this:
Public Class TfsEventSubscriber
Implements ITfsEventSubscriber
Public Sub Notify(ByVal eventXml As String, ByVal tfsIdentityXml As String) Implements ITfsEventSubscriber.Notify
End Sub
End Class
Writing the Implementation
You can now extend the Notify method to provide your service’s implementation. In our example we simply dump each request out as a uniquely named XML file:
Imports System.IO
Public Class TfsEventSubscriber
Implements ITfsEventSubscriber
Public Sub Notify(ByVal eventXml As String, ByVal tfsIdentityXml As String) Implements ITfsEventSubscriber.Notify
File.WriteAllText(Path.Combine(Path.GetTempPath(), Guid.NewGuid.ToString() & ".xml"), eventXml)
End Sub
End Class
Configure the WCF Service
There are a number of changes that need to be made to the WCF Service’s configuration, these can be made by editing the app.config file directly or by using the WCF Configuration Editor shown here.
We need to change the binding from wsHttpBinding to basicHttpBinding.
Because Team Foundation Server will never access the metadata service we can safely remove it from our configuration. Firstly we delete the endpoint that implements the IMetadataExchange contract.
Then we remove the serviceMetadata behaviour:
Testing and Debugging Your Service
If you run the project using Visual Studio it will automatically host your service using the WcfSvcHost that ships with Visual Studio 2008. While the project is running in Visual Studio then all of the usually debugging tools (such as breakpoints) will be available.
Once the service is running you can use BisSubscribe.exe from the Visual Studio 2008 SDK to add a subscription to Team Foundation Server so that your service is called when events are raised. In this example we add a SOAP subscription to the BuildStatusChangedEvent using the URL that Visual Studio hosted our service:
BisSubscribe.exe /eventType BuildStatusChangedEvent
/address http://DEVWORKSTATION:8731/Design_Time_Addresses/ReceiveTfsEventsUsingWcf/TfsEventSubscriber/
/server http://TFSRTM08:8080/
/deliveryType Soap
If you have a firewall, including the one that ships with Windows, you may need to disable it or add an exception for the port that the service has been hosted on.
Deploying Your Service
Once you’ve built and tested and your service you can host it using any of the hosting methods supported by WCF (such as using IIS or self-hosting) and then use BisSubscribe to add the necessary subscriptions.