Click here to Skip to main content
Click here to Skip to main content

Creating your own event handler for the TFS Event Handler Service

By , 21 Aug 2007
 

Introduction

Creating an event handler with the Team Foundation Server Event Handlers is very easy. You will need to inherit from the AEventHandler class which is part of the RDdotNet.TeamFoundation.dll located in the install directory (I will make an SDK later).

image

The code

Imports Microsoft.TeamFoundation.Client

Public MustInherit Class AEventHandler(Of TEvent)

 Public MustOverride Sub Run(ByVal EventHandlerItem As EventHandlerItem(Of TEvent), _ 
                             ByVal ServiceHost As ServiceHostItem, _
                             ByVal TeamServer As TeamServerItem, _
                             ByVal e As NotifyEventArgs(Of TEvent))

 Public MustOverride Function IsValid(ByVal EventHandlerItem As EventHandlerItem(Of TEvent), _
                                      ByVal ServiceHost As ServiceHostItem, _
                                      ByVal TeamServer As TeamServerItem, _
                                      ByVal e As NotifyEventArgs(Of TEvent)) As Boolean

End Class

Both of the methods that the AEventHandler exposes have the same signature. Hear is what it all means...

Name Type Description
EventHandlerItem EventHandlerItem(Of TEvent) The event handler item contains information about the handler. This includes an instance of the config data, the handler itself, and a status property. The TEvent generic makes sure that everything is strongly typed for the event that you are dealing with.
ServiceHost ServiceHostItem Base address of the service, event type, host config data, and a link to the service host object.
TeamServer TeamServerItem Config data for the team server as well as an instance of the Microsoft.TeamFoundation. TeamFoundationServer object.
e NotifyEventArgs(Of TEvent) This object allows access to all of the event specific data including the event object, the event type, the TFS Identity object, and the TFS Subscription object.

Let's look at the implementation that comes with the TFS Event Handler: the "AssignedToHandler". The IsValid method denotes whether the handler will actually run at all:

Public Overrides Function IsValid(_
       ByVal EventHandlerItem As EventHandlerItem(Of WorkItemChangedEvent), _
       ByVal ServiceHost As ServiceHostItem, _
       ByVal TeamServer As TeamServerItem, _
       ByVal e As NotifyEventArgs(Of WorkItemChangedEvent)) As Boolean
    If e.Event Is Nothing Then
        Return False
    End If
    Dim assignedName As String = WorkItemEventQuerys.GetAssignedToName(e.Event)
    If String.IsNullOrEmpty(assignedName) Then
        Return False
    Else
        Return Not assignedName = WorkItemEventQuerys.GetChangedByName(e.Event)
    End If
End Function

This method initially checks to see if the event exists, and then queries the assigned name from the event using a work item event query which consists of:

Return eventData.CoreFields.StringFields.Find(New _
       Predicate(Of StringField)(AddressOf FindAssignedTo)).NewValue

All this does is search the string fields associated with the core work item bits to find the "System.AssignedTo" value. You could do this manually, but I have a number of queries there and you can add any number you wish.

The logic: If event exists and the assigned name is not empty, then check that the assigned name is not the changed name.

So in English, with all of the crap split out: Did the user assign the work item to himself? If not, then send them an email!

The rest, as they say, is just logic. The "Run" method calls "IsValid" and then sends an email if it is in fact valid, hardly rocket science.

Hopefully, with this knowledge, you will be able to make many many event handlers!

Update

For the delayed CTP 1 of the TFS Event Handler, I have changed the logic quite a lot, but the same IsValid and Run methods exist. The parameters are, however, slightly different. I have taken into account security, and you will have to make your own connection to the TFS server using your own username and password. I have changed this to protect the security of the application as I want developers to be able to upload event handler assemblies and WF workflow without having to get access to the server. I have also changed it so the service that captures the events is not the same that runs the handlers. This allows me to send the events between these services using MSMQ, thus giving the service some much needed redundancy.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Martin Hinshelwood
Instructor / Trainer Northwest Cadence
United States United States
Member

Martin Hinshelwood is an Senior ALM Consultant at Northwest Cadence, but also a Microsoft Visual Studio ALM MVPs in Washington, US (Formally UK) and has over 9 years experience in the software industry. He is a member of the Visual Studio ALM Rangers and is also a Professional Scrum Trainer. He regularly writes on his Processes, Practices & Tools blog, and speaks often on Scrum, good practices and Visual Studio ALM.

 

Martin aims to provide Strategic and Tactical consulting on successful implementations of new Processes, Practices & Tools within both small and large organisations across the world. These would include, but not be limited to:

 
  • Scrum Mentoring & Training - Scrum Master Training, Scrum Developer Training, Scrum Mentoring, Scrum Coaching
  •  
  • Practice improvements – Requirements (Backlog,Limiting Work In Progress, Relative Complexity, Acceptance Criteria, Planning Poker, User Stories, Use Cases, Visualisation) , Teams (Team Protection, Commitment), Code (Unit Testing, Branching, Continuous Integration, TDD, ), Test (Acceptance Test Driven Development, Automation, ), Release & Configuration (Continuous Delivery, Continuous Deployment)
  •  
  • Visual Studio ALM Implementation, upgrades & Training - Visual Studio, Team Foundation Server, Team Foundation Build & Microsoft Test Manager

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 21 Aug 2007
Article Copyright 2007 by Martin Hinshelwood
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid