【问题标题】:Using the TFS 2010 API to subscribe to Workspace Events使用 TFS 2010 API 订阅工作区事件
【发布时间】:2011-07-08 05:48:13
【问题描述】:

我正在尝试编写一些代码来监视本地工作站上的 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);
        }


    }
}

【问题讨论】:

    标签: c# visual-studio-2010 tfs tfs-sdk


    【解决方案1】:

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

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

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

    【讨论】:

    • 公平地说,我认为当我操作工作区或在运行代码之外(例如从 VS 获取)时它们会触发,类似于 FileSystemWatcher 中的事件的行为方式。我将四处挖掘并寻找服务器端事件,看看这些是否符合我的要求。
    • 我已经查看了 API,但我认为没有任何东西可以满足我的需求。哦,好吧,回到绘图板
    • 好的。我已经使用了这个代码示例,它的工作原理和描述的一样好。但是,我需要更新我的应用程序数据库中的一些信息,其中任何更改都发生在服务器端(TeamWebAccess 或 WorkItem 在.net 中的应用程序之外更新)。感谢您的建议。
    • 这与 TFS 中的事件服务器有关,而不是客户端对象模型中的事件。你应该问一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多