首页 > 学院 > 开发设计 > 正文

TFS二次开发02——连接TFS

2019-11-17 03:17:34
字体:
来源:转载
供稿:网友

TFS二次开发02——连接TFS

Posted on 2014-03-15 17:12 祥叔 阅读(...) 评论(...) 编辑 收藏

在上一篇《TFS二次开发01——TeamPRojectsPicher》介绍了 TeamProjectsPicher 对象,使用该对象可以很简单的实现连接TFS。

但是如果我们要实现自定义的UI客户端 或者我们要做一个非WinForm版的TFS客户端(比如Web 或者WPF),那么TeamProjectsPicher 对象就无能为力了。那我们就只能自己实现了。

这篇文章主要介绍:

1:连接TFS Server

2:获取所有TfsTeamProjectCollection

3:获取某个TfsTeamProjectCollection 下的所有Team Project

添加命名空间

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation.Framework.Client;

using Microsoft.TeamFoundation.Framework.Common;

using Microsoft.TeamFoundation.VersionControl.Client;

1:连接TFS服务器

       //想要连接TFS,肯定先要知道TFS 的地址       string tfsServerURL = "http://192.168.83.70:8080/tfs";       Uri configurationServerUri = new Uri(tfsServerURL);       //与TFS服务器连接       TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri);

2:获取所有团队项目结合(TfsTeamProjectCollection)

        /// <summary>        /// 查询所有指定TFS服务上的所有TeamProjectCollection 信息        /// </summary>        /// <param name="tfsServerURL"></param>        /// <returns></returns>        List<TfsTeamProjectCollection> GetAllTeamProjectCollection(string tfsServerURL)        {            Uri configurationServerUri = new Uri(tfsServerURL);            //与TFS服务器连接            TfsConfigurationServer configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri);                      List<TfsTeamProjectCollection> lst = new List<TfsTeamProjectCollection>();            //TFS目录节点            CatalogNode configurationServerNode = configurationServer.CatalogNode;            //查询当前TFS下的所有TeamProjectCollection            ReadOnlyCollection<CatalogNode> tpcNodes = configurationServerNode.QueryChildren(                    new Guid[] { CatalogResourceTypes.ProjectCollection },                    false,                    CatalogQueryOptions.None);            //遍历每一个TeamProjectCollection 节点            foreach (CatalogNode tpcNode in tpcNodes)            {            //获取 当前 team project collection 名称.                String displayName = tpcNode.Resource.DisplayName;                // 获得 当前 team project collection 描述.                String description = tpcNode.Resource.Description;                        //获取当前 team project collection 的描述.                ServiceDefinition tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"];                ILocationService configLocationService = configurationServer.GetService<ILocationService>();                Uri tpcUri = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition));                // 真正的连接到team project collection                TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tpcUri);                lst.Add(tpc);            }            return lst;        }

TfsTeamProjectCollection 对象常用属性:

TfsTeamProjectCollection. CatalogNode. Resource.DisplayName // team project collection 名称.

TfsTeamProjectCollection .Name           // team project collection 名称.

TfsTeamProjectCollection .Uri             // team project collection Url 信息.

TfsTeamProjectCollection. CatalogNode. Resource. Description // team project collection 描述.

3:获取某个TfsTeamProjectCollection 下的所有Team Project信息

      TeamProject[] GetAllTeamProject(TfsTeamProjectCollection tpc)        {            List<TeamProject> lst = new List<TeamProject>();            VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer;            return version.GetAllTeamProjects(true);        }

由于我们在实际应用中连接到TFS时,只能连接一个TfsTeamProjectCollection 。所以如果我们知道了该TfsTeamProjectCollection的Url 那么就可以免去上面的步骤,代码乳如下:

        /// <summary>        /// 获取所有的TeamProject        /// </summary>        /// <returns></returns>        TeamProject[] GetAllTeamProject(string tpcURL)        {            //连接到team project collection ,使用此方式直接连接TFS时 TfsTeamProjectCollection. CatalogNode 为 none 。            //因为在使用时通常是只连接到一个Team project collection ,所有更多的时候我们使用这种方式连接TFS            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(tpcURL));            //版本控制            VersionControlServer version = tpc.GetService(typeof(VersionControlServer)) as VersionControlServer;            //获取所有TeamProject             return version.GetAllTeamProjects(true);        }

结合上面的代码可以实现如下图所示的效果:

4:创建团队项目

            TeamProjectFolderOptions project = new TeamProjectFolderOptions("project1");            version.CreateTeamProjectFolder(project);


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表