如何在TFS中正确调用GetWorkspace?

ast*_*ker 5 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);        
}
Run Code Online (Sandbox Code Playgroud)

我使用VS2015,并且从NuGet回购中获取了TFS库。它是“ Microsoft.TeamFoundationServer.ExtendedClient”版本“ 15.112.1”。

Pat*_*SFT 6

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);
Run Code Online (Sandbox Code Playgroud)

看看这个类似的问题:即使工作空间存在并且具有映射,Microsoft.TeamFoundation.VersionControl.Client.ItemNotMappedException