2

我正在尝试编写一些代码来监视本地工作站上的 TFS 工作区,但目前我在触发事件时遇到了问题。

例如,如果我在我的工作空间中映射一个新文件夹,我想订阅 versionControl.UpdatedWorkspace 事件,如果我执行“get”,我想映射到 versionControl.Getting 事件。下面的代码是一个我认为应该可以工作的控制台应用程序,但是当我这样做时,什么也没有发生。有谁知道如何成功订阅这些事件?

VS2010、TFS 2010、WinXP SP3

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

namespace TestEventHanling
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri serverUri = new Uri(@"http://TfsServer:8080/tfs/collection");

            using (TfsTeamProjectCollection collection = new TfsTeamProjectCollection(serverUri, CredentialCache.DefaultCredentials))
            {

                VersionControlServer versionControl = (VersionControlServer)collection.GetService(typeof(VersionControlServer));
                versionControl.UpdatedWorkspace += new WorkspaceEventHandler(OnUpdatedWorkspace);
                versionControl.Getting += new GettingEventHandler(OnGetting);

                Console.WriteLine("Press \'q\' to quit.");
                while (Console.Read() != 'q') ;

            }
        }


        internal static void OnUpdatedWorkspace(object sender, WorkspaceEventArgs e)
        {
            foreach (WorkingFolder wf in e.Workspace.Folders)
            {
                Console.WriteLine("Workspace updated {0}", wf.ServerItem);
            }
        }

        internal static void OnGetting(Object sender, GettingEventArgs e)
        {
            Console.WriteLine("Getting: {0}, status: {1}", e.TargetLocalItem, e.Status);
        }


    }
}
4

1 回答 1

1

我的理解是,这些是您本地 VersionControlServer 实例上的事件。也就是说,当您在代码中对该实例进行操作时,它们将触发。

例如,如果在代码中的其他地方更新了工作区,则UpdatedWorkspace处理程序将触发。

您可以在服务器端订阅一组较小的事件(签入、构建等),但我不确定您是否可以通过 VersionControlServer 类监视服务器上发生的事情。

于 2011-03-04T15:57:25.200 回答