Team Foundation Server – Eventing Service - Part 2 (Publishing events)
In the first part of this article we discussed about how to subscribe to the events which are published by the Team Foundation Server (TFS). Although the events published by TFS are quite extensive, there still may exist need to subscribe to the events which are not covered by TFS. To publish such events we need to create a separate event class and an application to raise that event.
This article discusses:
- How to publish an event which can be subscribed to
- How to raise that event from an application with certain event related data
- How to access the event related data
Events which are to be published are loosely coupled with the subscribers. The responsibility of notification of the event being raised is with the TFS. The notification service of TFS needs the .xsd file representing the event class to be present at a specific location in the file system. We begin creating that file by first creating a class library which contains the class which represents the event. This class has certain restrictions
1. It should have a default public constructor, a constructor that does not accept any parameter.
2. It also will have public fields which will be assigned values during raising the event.
3. The class should be serializable i.e. should have Serializable attribute.
The example of such a class is given below:
public class WorkItemAddedEventType
public string id;
public string projectUri;
Public Class WorkItemAddedEventType
Public Sub New()
Public id As String
Public projectUri As String
Once the class is compiled into a component and a .dll is created, we can use the tool XSD.exe to convert the class to the respective .xsd file. The syntax of the command is as follows:
>XSD.exe MyComponent.dll /type:WorkItemAddedEventType /out:DirName
This command generates the schema file representing the event class. The generated schema file for the above written class would look like this:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="WorkItemAddedEventType" nillable="true" type="WorkItemAddedEventType" />
<xs:element minOccurs="0" maxOccurs="1" name="id" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="projectUri" type="xs:string" />
This generated XSD file is stored at …\Program Files\Microsoft Visual Studio 2005 Team Foundation Server\Web Services\Services\v1.0\Transforms with the same name as the class name which in this case would result in to a file named WorkItemAddedEventType.xsd.
Once the file has been stored at the mentioned location, notification service of TFS will come to know about it after IIS is restarted. Execute the >iisreset.exe command to do that. Now anyone can subscribe to this event.
Raising the published events
The event that is published can now be raised using the Team Foundation Object Model. We can create a component or a windows desktop application to raise the event. In that application we need to give the reference to following dlls:
3. The dll which contains the event class.
4. Any additional service which we are using for example in this example since we are adding a workitem, we need to give reference to Microsoft.TeamFoundation.WorkItemTracking.Client.dll
We will need now to create an instance of IEventService and call the FireAsyncEvent method of that instance to start the notification to the subscribers. The code to do that is in the listing below:
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("http://SEED_tfs:8080");
IEventService EventService = (IEventService)tfs.GetService(typeof(IEventService));
WorkItemAddedEventType WIAddedEv = new WorkItemAddedEventType();
WIAddedEv.id = wi.Id.ToString();
//wi is the instance of new workitem being added
WIAddedEv.projectUri = proj; // proj is the name of the team project
The fields which are given values here can be accessed using the template as was given in the Part 1 of this article. The template which will be used to create the email may look like this:
<title>TFS Workitem Tracking Subsystem Notification</title>
<body style="font-family:courier new; font-size:12px;">
<div>WorkItem has been added</div>
WorkItem ID: <variable name="id" />
<variable name="projectUri" />
In this format the variable tag extracts the data from the event arguments and displays it.
In this article we have seen how to write your own events, how to publish them and how to raise the events which will be notified to the subscribers. This mechanism provides excellent way to extending the functionality for automation of tasks involved with TFS.
I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!