本码农最近开发一个VS扩展,其中有些功能涉及到文件的签出。我们公司用的是TFS,遇到了一些奇特的现象,将解决过程记录如下。

一、明明在线的连接却Offline属性等于True

 public static Workspace GetWorkspace(string slnDir)
{ var projectCollections = new List<RegisteredProjectCollection>((RegisteredTfsConnections.GetProjectCollections()));
var onlineCollections = projectCollections.Where(c => !c.Offline).ToList(); // fail if there are no registered collections that are currently on-line
if (!onlineCollections.Any())
{
return null;
}
Workspace workspace = null;
// find a project collection with at least one team project
foreach (var registeredProjectCollection in onlineCollections)
{
var projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(registeredProjectCollection);
projectCollection.EnsureAuthenticated();
var versionControl = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));
var teamProjects = new List<TeamProject>(versionControl.GetAllTeamProjects(false));
// if there are no team projects in this collection, skip it
if (teamProjects.Count < ) continue; var dir = new DirectoryInfo(slnDir);
while (workspace == null)
{
workspace = versionControl.TryGetWorkspace(dir.FullName);
if (dir.Parent == null)
break;
dir = dir.Parent;
} if (workspace != null && workspace.HasUsePermission)
break;
}
return workspace;
}

 现象就是上面这段代码中

var onlineCollections = projectCollections.Where(c => !c.Offline).ToList();

一句找不到想要的在线的TFS Server

首先尝试了删除Local AppData中(C:\Users\*\AppData\Local\Microsoft\Team Foundation)的Cache,不起作用。

然后找到注册表

HKEY_USERS\S-1-5-21-2532103873-3336248781-2863026242-1503\Software\Microsoft\VisualStudio\11.0\TeamFoundation\Instances\tfs.yintai.org\Collections\PlatformCollection
将此节点下Offline改为0

可以了。

原因不明,可能跟连接了多个TFS有关。

二、TryGetWorkspace方法找不到对应的Workspace

然后又遇到

workspace = versionControl.TryGetWorkspace(dir.FullName);

总是返回null

后改为使用QueryWorkspaces方法遍历所有workspace解决

将这段

 var dir = new DirectoryInfo(slnDir);
while (workspace == null)
{
workspace = versionControl.TryGetWorkspace(dir.FullName);
if (dir.Parent == null)
break;
dir = dir.Parent;
}

替换为

  try
{
Workspace[] workspaces = versionControl.QueryWorkspaces(null, Environment.UserName,
Environment.MachineName);
foreach (var ws in workspaces)
{
foreach (var folder in ws.Folders)
{
if (slnDir.StartsWith(folder.LocalItem))
{
workspace = ws;
break;
}
}
if (workspace != null && workspace.HasUsePermission)
break;
}
}
catch
{
continue;
}
  

最新文章

  1. AutoMapper(七)
  2. nginx反向代理+集群
  3. JAVA多线程实现的四种方式
  4. js 获取 根目录
  5. BZOJ-3211花神游历各国 并查集+树状数组
  6. C语言中 scanf 和 printf 的小要点
  7. 在游戏中使用keybd_event的问题
  8. JS三元运算符
  9. js方法中的this
  10. poj2096--Collecting Bugs(可能性dp第二弹,需求预期)
  11. IOS 9人机界面指南(1)
  12. linux 套接字编程入门--Hello World
  13. CSS书写规范与理论
  14. APNS IOS 消息推送处理失效的Token
  15. 深度揭秘腾讯云TSF日调用量超万亿次背后技术架构
  16. SQL语句删除字段,改变字段长度
  17. springboot use
  18. centos7 下 安装部署nginx
  19. bootstrap-table初使用
  20. ab压测札记(Apache Bench)

热门文章

  1. Android SectionIndexer 的使用(联系人分类索引)
  2. 演示:纯CSS实现自适应布局表格
  3. hadoop2.0安装和配置
  4. 教程:在 VM Depot 中查找 Azure 可用的虚拟机镜像
  5. JPush三分钟之后的事 fragmentActivity(一)
  6. Unity Diffuse Metal Shader Mod
  7. 自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客
  8. Hadoop Hive概念学习系列之什么是Hive?(一)
  9. N2N 对等VPN网络
  10. java 启用新线程异步调用