-->

Using the TFS 2010 API to subscribe to Workspace E

2019-05-21 16:30发布

问题:

I'm trying to write some code that monitors the TFS workspace(s) on my local workstation but at the moment I'm having problems getting the events to fire.

For example if I map a new folder in my workspace I want to subscribe to the versionControl.UpdatedWorkspace event, and if I do a “get” I want to map to the versionControl.Getting event. The code below is a console application that I think should work, but when I do a get nothing happens. Does anyone know how to successfully subscribe to these events?

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);
        }


    }
}

回答1:

My understanding are that these are events that are on your local instance of VersionControlServer. That is to say, they will fire when you act on that instance in your code.

For example, if, somewhere else in your code, you updated a workspace, then the UpdatedWorkspace handler would fire.

There's a smaller set of events that you can subscribe to server-side (check-in, builds, etc.), but I'm not sure that you can monitor what's happening on the server through the VersionControlServer class.