如何正确调用TFS中的GetWorkspace?

时间:2017-09-29 07:24:59

标签: c# tfs

目前,当我调用GetWorkspace时,我得到ItemNotMappedException异常,但是当我手动迭代工作区时,我可以使代码正常工作。这是如此奇怪,我想知道在我致电GetWorkspace之前是否应该打电话给某些刷新事件?

这是我的代码:

Workspace active = null;
using (var tfs = new TfsTeamProjectCollection(uri))
{
    tfs.EnsureAuthenticated();
    VersionControlServer vcs = tfs.GetService<VersionControlServer>();

    // displaying info and manually finding desired workspace
    foreach (var ws in vcs.QueryWorkspaces(null, vcs.AuthorizedUser, Environment.MachineName))
    {
        Console.WriteLine(ws.Name);
        foreach (var f in ws.Folders)
        {
            Console.WriteLine($"  {f.LocalItem}");
            if (f.LocalItem == map_path)
                active = ws;
        }
    }

    // at this point workspace is found and I can work with it

    // but this will crash
    Workspace workspace = vcs.GetWorkspace(map_path);        
}

我使用VS2015,从NuGet repo获取TFS库。它是“Microsoft.TeamFoundationServer.ExtendedClient”版本“15.112.1”。

1 个答案:

答案 0 :(得分:5)

VersionControlServer.GetWorkspace方法搜索当前计算机上的所有已知工作空间,以识别明确或隐式映射提供的本地路径的工作空间。如果未找到工作空间,则此方法将抛出ItemNotMappedException

尝试在代码中调用Workstation.EnsureUpdateWorkspaceInfoCache以强制更新工作区信息缓存:

TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("<your-tfs-uri-here>"));
VersionControlServer tfServer = tpc.GetService<VersionControlServer>();
Workstation.Current.EnsureUpdateWorkspaceInfoCache(tfServer, tfServer.AuthorizedUser);

看看这个类似的问题:Microsoft.TeamFoundation.VersionControl.Client.ItemNotMappedException even when the workspace exists and has a mapping