简体   繁体   English

使用TFS 2010 API订阅WorkItem事件

[英]Using the TFS 2010 API to subscribe to WorkItem Events

I'm trying to write some code that monitors the TFS WorkItems on my local workstation but at the moment I'm having problems getting the events to fire. 我正在尝试编写一些代码来监视本地工作站上的TFS WorkItem,但此刻我在触发事件方面遇到了问题。

I have subscribed to FieldChange event of WorkItem but it doesn't fire when I change/update any workitem. 我已经订阅了WorkItem的FieldChange事件,但是当我更改/更新任何工作项时它不会触发。

The code below is a console application that I think should work,bu it doesn't . 下面的代码是一个控制台应用程序,我认为应该可以,但不能。 Does anyone know how to successfully subscribe to these events? 有人知道如何成功订阅这些活动吗?

Any help in this matter is appreciable . 在这方面的任何帮助都是可观的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using System.Net;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Framework.Client;

namespace TFSEvents
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Uri serverUri = new Uri(@"http://tfs");
                string username, password;
                Console.WriteLine("Enter Username:");
                username = Console.ReadLine();
                Console.WriteLine("Enter password:");
                password = ReadPassword();
                NetworkCredential cred = new NetworkCredential(username, password);
                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(serverUri, cred);
                tfs.EnsureAuthenticated();
                IEventService vs = tfs.GetService<IEventService>();
                var wiww = tfs.GetService<WorkItemStore>();
                var wi = wiww.GetWorkItem(4671);
                wi.FieldChanged += new WorkItemFieldChangeEventHandler(changeHandler);
                var x = vs.GetAllEventSubscriptions().ToList();
                Console.WriteLine("Press \'q\' to quit.");
                while (Console.ReadLine() != "q") ;
            }
            catch (Exception e)
            {

            }
        }

        private static void changeHandler(object o, WorkItemEventArgs e)
        {
            Console.WriteLine(e.Field);
        }
   }
}

You cant handle WorkItem events in that way, it will only work for a single instance. 您不能以这种方式处理WorkItem事件,它将仅适用于单个实例。 There are only a fixed set of events you can handel: 您只能处理一组固定的事件:

http://nkdagility.com/tfs-event-handler-for-team-foundation-server-2010/ http://nkdagility.com/tfs-event-handler-for-team-foundation-server-2010/

And you need an eventhandler to use them: 您需要一个事件处理程序来使用它们:

http://nkdagility.com/tfs-event-handler-in-net-3-5-part-2-handling-team-foundation-server-events/ http://nkdagility.com/tfs-event-handler-in-net-3-5-part-2-handling-team-foundation-server-events/

UPDATE UPDATE

If you don't want to use BisSubscribe.exe you can create a subscription using the API. 如果您不想使用BisSubscribe.exe,则可以使用API​​创建订阅。 If you check my bad code on http://tfseventhandler.codeplex.com/SourceControl/latest#RELEASE/v1.3/RDdotNet.TeamFoundation/Managers/TeamServerManager.vb you should see a "RegisterEvent()" method that will do the job. 如果您在http://tfseventhandler.codeplex.com/SourceControl/latest#RELEASE/v1.3/RDdotNet.TeamFoundation/Managers/TeamServerManager.vb上检查了我的错误代码,则应该看到一个“ RegisterEvent()”方法,该方法将工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM