TFS Event Subscriber 预订管理
注意:代码是针对Beta3 Refresh测试的。
微软TFS提供Event Subscriber,简单说就是TFS任何操作都可以触发一事件,这些事件包含CheckIn,WorkItemChange,BuildComplete等等。
微软现在提供两种预订,一种是通过邮件的方式传递,一种是通过WebService。邮件方式设置简单,无需编码,只要简单设置即可,但是不够灵活,WebService需要编码,但是可以任意操作,非常灵活,如果你有LCS即时通讯服务器,你可以调用LCS 的API来发送即时消息给你的项目组员。
这里主要描述WebService方式:
这种方式包含两部分的内容:一个预订管理(Subscriber),一个是WebService接受端。
微软的WebService方式目前的版本是没有界面可以操作的,所以要自己调用API来实现,(API还是比较简单的,不要看了API就怕)。
运行的Subscriber:
Subscriber的主要代码如下:
public static int SubscribeEvent(string tfsServer, string userID, string eventType, string filterExpression, string deliveryAddress, string deliveryType, string deliverySchedule)
{
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(tfsServer);
IEventService eventService = tfs.GetService(typeof(IEventService)) as IEventService;
DeliveryPreference delivery = new DeliveryPreference();
delivery.Type = deliveryType == "EmailHtml" ? DeliveryType.EmailHtml :
deliveryType == "EmailPlaintext" ? DeliveryType.EmailPlaintext :
deliveryType == "Soap" ? DeliveryType.Soap : DeliveryType.Soap ;
delivery.Schedule = deliverySchedule == "Daily" ? DeliverySchedule.Daily
: deliverySchedule == "Immediate" ? DeliverySchedule.Immediate
: deliverySchedule == "Weekly" ? DeliverySchedule.Weekly
: DeliverySchedule.Immediate;
delivery.Address = deliveryAddress;
int id = eventService.SubscribeEvent(userID, eventType, filterExpression, delivery);
if (id < 0)
throw new ApplicationException("Unable to subscribe");
return id;
}
{
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(tfsServer);
IEventService eventService = tfs.GetService(typeof(IEventService)) as IEventService;
DeliveryPreference delivery = new DeliveryPreference();
delivery.Type = deliveryType == "EmailHtml" ? DeliveryType.EmailHtml :
deliveryType == "EmailPlaintext" ? DeliveryType.EmailPlaintext :
deliveryType == "Soap" ? DeliveryType.Soap : DeliveryType.Soap ;
delivery.Schedule = deliverySchedule == "Daily" ? DeliverySchedule.Daily
: deliverySchedule == "Immediate" ? DeliverySchedule.Immediate
: deliverySchedule == "Weekly" ? DeliverySchedule.Weekly
: DeliverySchedule.Immediate;
delivery.Address = deliveryAddress;
int id = eventService.SubscribeEvent(userID, eventType, filterExpression, delivery);
if (id < 0)
throw new ApplicationException("Unable to subscribe");
return id;
}
WebService的主要代码如下:
[SoapDocumentMethod("http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/02/Notify",
RequestNamespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/02")]
[WebMethod]
public void Notify(string eventXml, string tfsIdentityXml)
{
string stylesheet = Server.MapPath("WorkItemChangedEvent.xsl");
try
{
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(eventXml);
//xdoc.Save("c:\\test.xml"); //test
XslCompiledTransform xsldoc = new XslCompiledTransform();
xsldoc.Load(stylesheet);
XPathNavigator nav = xdoc.CreateNavigator();
nav.MoveToRoot();
StringWriter writer = new StringWriter();
xsldoc.Transform(nav, null, writer);
//System.Diagnostics.EventLog.WriteEntry("TFS", eventhtml); //log
string account = @"domain\test";
string password = "test";
string uri = "sip:test@domain.local";
string strMsgHeader = null;
string strDestURI = "cleo@domain.local";
string strMsg = writer.ToString();
CMS.LCSLib.RTCClass.SendMessage(account, password, uri, strMsgHeader, strMsg, strDestURI); //
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("TFS", stylesheet + ex.ToString());
}
}
RequestNamespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/02")]
[WebMethod]
public void Notify(string eventXml, string tfsIdentityXml)
{
string stylesheet = Server.MapPath("WorkItemChangedEvent.xsl");
try
{
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(eventXml);
//xdoc.Save("c:\\test.xml"); //test
XslCompiledTransform xsldoc = new XslCompiledTransform();
xsldoc.Load(stylesheet);
XPathNavigator nav = xdoc.CreateNavigator();
nav.MoveToRoot();
StringWriter writer = new StringWriter();
xsldoc.Transform(nav, null, writer);
//System.Diagnostics.EventLog.WriteEntry("TFS", eventhtml); //log
string account = @"domain\test";
string password = "test";
string uri = "sip:test@domain.local";
string strMsgHeader = null;
string strDestURI = "cleo@domain.local";
string strMsg = writer.ToString();
CMS.LCSLib.RTCClass.SendMessage(account, password, uri, strMsgHeader, strMsg, strDestURI); //
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("TFS", stylesheet + ex.ToString());
}
}
测试:任意修改TFS任意项目的WorkItem,就可以接受的信息: