Team Foundation Server – Eventing Service - Part 2 (Publishing events)

Posted by: Subodh Sohoni , on 4/16/2008, in Category VSTS & TFS (Azure DevOps)
Views: 24088
Abstract: 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.
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
 
Publishing events
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:
C#
using System;
using System.Collections.Generic;
using System.Text;
 
namespace WorkItemAddedEvent
{
    [Serializable]
    public class WorkItemAddedEventType
    {
        public WorkItemAddedEventType()
        { }
        public string id;
        public string projectUri;
    }
}
 
VB.NET
Imports System
Imports System.Collections.Generic
Imports System.Text
 
Namespace WorkItemAddedEvent
      <Serializable> _
      Public Class WorkItemAddedEventType
            Public Sub New()
            End Sub
            Public id As String
            Public projectUri As String
      End Class
End Namespace
 
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:
<?xml version="1.0"encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="WorkItemAddedEventType" nillable="true" type="WorkItemAddedEventType" />
      <xs:complexType name="WorkItemAddedEventType">
            <xs:sequence>
                  <xs:element minOccurs="0" maxOccurs="1" name="id" type="xs:string" />
                  <xs:element minOccurs="0" maxOccurs="1" name="projectUri" type="xs:string" />
            </xs:sequence>
      </xs:complexType>
</xs:schema>
 
 
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:
1.       Microsoft.TeamFoundation.dll
2.       Microsoft.TeamFoundation.Client.dll
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:
C#
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
EventService.FireAsyncEvent(WIAddedEv); 
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:
<html>
      <head>
            <title>TFS Workitem Tracking Subsystem Notification</title>
      </head>
      <body style="font-family:courier new; font-size:12px;">
            <div>WorkItem has been added</div>
            <br />
            <div>
                  WorkItem ID: <variable name="id" />
            </div>
            <divTeam Project:=""
                  <variable name="projectUri" />
                  </div>
            </body>
</html>
 
In this format the variable tag extracts the data from the event arguments and displays it.
 Conclusion
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.
If you liked the article,  Subscribe to my RSS Feed.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

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 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 eBook 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 .NET Standard and the upcoming C# 8.0 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!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Subodh is a consultant and corporate trainer. He has overall 28+ years of experience. His specialization is Application Lifecycle Management and Team Foundation Server. He is Microsoft MVP – VS ALM, MCSD – ALM and MCT. He has conducted more than 300 corporate trainings and consulting assignments. He is also a Professional SCRUM Master. He guides teams to become Agile and implement SCRUM. Subodh is authorized by Microsoft to do ALM Assessments on behalf of Microsoft. Follow him on twitter @subodhsohoni


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Dan Libre on Monday, April 21, 2008 1:19 AM
To the point content..keep it up guys!! Which version does the article target? TFS 2005 or the latest one 2008?
Comment posted by Subodh Sohoni on Tuesday, April 22, 2008 12:24 PM
Hi Dan,
Thanks for your comments. The above article target TFS 2005 although it may also work for 2008 without any change. I have not tried it on 2008 yet but may do so in a few days.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook