Moving to a deploy system based on Tfs Build

Now that I’m able to deploy to a remote machine a web application thanks to a customized build workflow it is time to move to a real scenario. I’ve blogged about two distinct tasks

  1. executing arbitrary code with a tfs build
  2. deploy an application to a remote server with a custom tfs workflow

Now I want to move to a real scenario: executing the deploy when build quality of a specific build changes to “ready for deployment”. To accomplish this task you first need to understand how to register for event subscription in TFS, thanks to the bisubscribe.exe tool. Subscription is made through webservices.


You register to TFS event submitting ad url where your service is listening, and whenever the event fires, TFS calls your service. To find all events you can register to, you can simply browse the service


Then click GetRegistrationEntries, and invoke the service. The result value is a long XML where you can find the eventType node that specifies the type of events you can register to. Since I want to be notified when a build changes quality, I need to register to BuildStatusChangeEvent

   1: - <EventType>

   2:   <Name>BuildStatusChangeEvent</Name>

   3:   <Schema><?xml version="1.0" encoding="utf-8"?>

   4:     <xs:schema elementFormDefault="qualified" xmlns:xs="">

   5:       <xs:element name="BuildStatusChangeEvent" nillable="true" type="BuildStatusChangeEvent" />

   6:       <xs:complexType name="BuildStatusChangeEvent">

   7:         <xs:sequence>

   8:           <xs:element minOccurs="0" maxOccurs="1" name="TeamFoundationServerUrl" type="xs:anyURI" />

   9:           <xs:element minOccurs="0" maxOccurs="1" name="TeamProject" type="xs:string" />

  10:           <xs:element minOccurs="0" maxOccurs="1" name="Title" type="xs:string" />

  11:           <xs:element minOccurs="0" maxOccurs="1" name="Subscriber" type="xs:string" />

  12:           <xs:element minOccurs="0" maxOccurs="1" name="Id" type="xs:string" />

  13:           <xs:element minOccurs="0" maxOccurs="1" name="Url" type="xs:anyURI" />

  14:           <xs:element minOccurs="0" maxOccurs="1" name="TimeZone" type="xs:string" />

  15:           <xs:element minOccurs="0" maxOccurs="1" name="TimeZoneOffset" type="xs:string" />

  16:           <xs:element minOccurs="0" maxOccurs="1" name="ChangedTime" type="xs:string" />

  17:           <xs:element minOccurs="0" maxOccurs="1" name="StatusChange" type="Change" />

  18:           <xs:element minOccurs="0" maxOccurs="1" name="ChangedBy" type="xs:string" />

  19:         </xs:sequence>

  20:       </xs:complexType>

  21:       <xs:complexType name="Change">

  22:         <xs:sequence>

  23:           <xs:element minOccurs="0" maxOccurs="1" name="FieldName" type="xs:string" />

  24:           <xs:element minOccurs="0" maxOccurs="1" name="OldValue" type="xs:string" />

  25:           <xs:element minOccurs="0" maxOccurs="1" name="NewValue" type="xs:string" />

  26:         </xs:sequence>

  27:       </xs:complexType>

  28:     </xs:schema>

  29:   </Schema>

  30: </EventType>

The command I issue to the tfs service to register my service for changing of BuildStatusChangeEvent is the following one.

   1: C:\Program Files\Microsoft Team Foundation Server 2010\Tools>

   2:     BisSubscribe.exe 

   3:         /EventType BuildStatusChangeEvent 

   4:         /address 

   5:         /collection http://win-y4onzs094up:8080/tfs/DefaultCollection


   7: BisSubscribe - Team Foundation Server BisSubscribe Tool

   8: Copyright (c) Microsoft Corporation.  All rights reserved.


  10: TF50001:  Created or found an existing subscription. The subscription ID is 2.

You need simply to specify: EventType, address and collection and you are done. Now for those ones that have no idea on how to implement the BuildMachine.svc service here is the details.

First of all check this post that explains in detail how to use WCF to create a service that is compatible with TFS subscription system, then it is matters of minute because the service needs only a function called Notify. First of all I created service interface

   1: namespace Bisubscribe.Test.Services

   2: {

   3:     [ServiceContract(Namespace = "")]

   4:     public interface INotificationService

   5:     {


   7:         [OperationContract(Action = "")]

   8:         [XmlSerializerFormat(Style = OperationFormatStyle.Document)]

   9:         void Notify(string eventXml, string tfsIdentityXml);


  11:     }

  12: }

Then the concrete class to simply dump content of the messages to verify that the event system is called.

   1: namespace Bisubscribe.Test.Services

   2: {

   3:     public class BuildMachine : INotificationService

   4:     {

   5:         private ILogger Logger { get; set; }


   7:         public BuildMachine(ILogger logger)

   8:         {

   9:             Logger = logger;

  10:         }


  12:         public void Notify(string eventXml, string tfsIdentityXml)

  13:         {

  14:             Logger.Debug("Called eventXML:\n{0}",  eventXml );

  15:             Logger.Debug("Called tfsIdentityXml:\n{0}", tfsIdentityXml);

  16:         }

  17:     }

  18: }

Since I’m using castle log4net integration and castle wcf integration I’m able to declare dependency to a ILogger interface to simply log every information that the server receives. Then I create a simple web application and insert the BuildMachine.Svc file with this content

   1: <%
   1:  @ServiceHost Service="Bisubscribe.Test.Services.BuildMachine" 

   2:     Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" 


I’m using the Castle WcfIntegration facility so I need to register the class in the castle config file, and finally, the only really critical step, is creating the right configuration for WCF to expose the service with SOAP 1.2 as described in this article. Now I changed the build quality of one of the build and check log file to verify if service is called. Here is the result.

   1: 2010-08-07 11:14:39,666 [7] DEBUG Bisubscribe.Test.Services.BuildMachine [(null)] - Called eventXML:

   2: <?xml version="1.0" encoding="utf-16"?><BuildStatusChangeEvent xmlns:xsi="" xmlns:xsd="">

   3:   <BuildUri>vstfs:///Build/Build/34</BuildUri>

   4:   <TeamFoundationServerUrl>http://win-y4onzs094up:8080/tfs/DefaultCollection</TeamFoundationServerUrl>

   5:   <TeamProject>WebDeploy</TeamProject>

   6:   <Title>WebDeploy Build Demo_20100709.7 Quality Changed To Initial Test Passed</Title>

   7:   <Subscriber>WIN-Y4ONZS094UP\Administrator</Subscriber>

   8:   <Id>Demo_20100709.7</Id>

   9:   <Url>http://win-y4onzs094up:8080/tfs/web/build.aspx?pcguid=ab718f2c-b4ab-499e-98c1-0a9766e3ddf6&amp;builduri=vstfs:///Build/Build/34</Url>

  10:   <TimeZone>Pacific Daylight Time</TimeZone>

  11:   <TimeZoneOffset>-07:00:00</TimeZoneOffset>

  12:   <ChangedTime>8/7/2010 2:12:37 AM</ChangedTime>

  13:   <StatusChange>

  14:     <FieldName>Quality</FieldName>

  15:     <OldValue>Ready for Deployment</OldValue>

  16:     <NewValue>Initial Test Passed</NewValue>

  17:   </StatusChange>

  18:   <ChangedBy>WIN-Y4ONZS094UP\gianmaria.ricci</ChangedBy>

  19: </BuildStatusChangeEvent> 

This is simple XML that can be parsed with Linq2SQL, you can find the id of the build and in StatusChange node you are notified of the field name that changes (Quality) the old and new value and this is enough information to implement the requested functionality.

Next step is triggering the execution of the remote build script to deploy the right build into the test server.


Sample code is here.

Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

3 thoughts on “Moving to a deploy system based on Tfs Build”

  1. Yes, TfsDeployer is a really cool solution to deploying application with TFS.
    I’m just showing how can you use TFS build to execute custom workflow that does different task than just building :), and I choose to show how to deploy an application :P. For production server TfsDeployer is surely a full featured product :)


Comments are closed.