You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6+ 下程序化同步TFS工作区的适配问题求助

问题:.NET 6+ 迁移 TFVC 工作区同步代码失败

原.NET 4.8环境下,配合NuGet包Microsoft.TeamFoundationServer.ExtendedClient版本16.205.1,以下代码可正常运行:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.Common;

namespace TfsWorkspaceGet
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create VSS credentials
            VssBasicCredential vssCred = new VssBasicCredential(
                "valid userid", 
                "valid PAT token");

            // connect to TFS
            var tfsCollection = new TfsTeamProjectCollection(new Uri("https://someorg.com/tfs/projects"));
            tfsCollection.EnsureAuthenticated();

            //connect to Azure DevOps using the basic credentials
            var connection = new VssConnection(new Uri(tfsUrl), vssCred);

            var versionControlServer = tfsCollection.GetService<VersionControlServer>();

            // Create a workspace
            Workspace workspace = versionControlServer.CreateWorkspace("tmpTfsWorkspace");

            // Map the server path to the local path
            workspace.CreateMapping(new WorkingFolder(
                "$/SomProj/Mainline/DEV/SomeComponent", 
                @"D:\WS\SomProj\Mainline\DEV\SomeComponent"));

            // Get the latest version
            workspace.Get();
        }
    }
}

升级至.NET 6+环境并更换NuGet包为兼容版本Microsoft.TeamFoundationServer.ExtendedClient 19.225.1后,出现多个类缺失:

  • TfsTeamProjectCollection
  • GetService
  • Workspace

尝试使用connection.GetClient<TfvcHttpClient>但无法实现工作区配置与同步需求,寻求解决方案。


解决方案

在新版Azure DevOps .NET SDK中,旧的TFS客户端模型(依赖.NET Framework的TfsTeamProjectCollection、Workspace等类)已被REST API客户端替代,需改用TFVC相关的Web API实现功能。

1. 安装必要NuGet包

确保安装以下包(Microsoft.TeamFoundationServer.ExtendedClient已包含这些,但单独安装更清晰):

  • Microsoft.VisualStudio.Services.Client
  • Microsoft.TeamFoundationServer.Client

2. .NET 6+ 兼容代码示例

using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using System.IO;
using System.Linq;

var patToken = "valid PAT token";
var tfsUrl = new Uri("https://someorg.com/tfs/projects");
var serverPath = "$/SomProj/Mainline/DEV/SomeComponent";
var localPath = @"D:\WS\SomProj\Mainline\DEV\SomeComponent";

// 初始化连接
var credentials = new VssBasicCredential(string.Empty, patToken);
var connection = new VssConnection(tfsUrl, credentials);

// 获取TFVC客户端
var tfvcClient = connection.GetClient<TfvcHttpClient>();

// 创建工作区并设置映射
var workspaceParams = new TfvcWorkspaceCreateParameters
{
    Name = "tmpTfsWorkspace",
    Owner = "valid userid",
    Mappings = new()
    {
        new TfvcMapping
        {
            ServerItem = serverPath,
            LocalItem = localPath
        }
    }
};
await tfvcClient.CreateWorkspaceAsync(workspaceParams);

// 同步服务器文件到本地(替代原Workspace.Get())
var items = await tfvcClient.GetItemsAsync(
    path: serverPath,
    recursionLevel: VersionControlRecursionType.Full,
    includeContent: true);

foreach (var item in items.Where(i => i.ItemType == ItemType.File))
{
    // 计算本地文件路径
    var localFilePath = item.ServerItem.Replace(serverPath, localPath);
    // 创建父文件夹
    Directory.CreateDirectory(Path.GetDirectoryName(localFilePath));
    // 写入文件内容
    File.WriteAllBytes(localFilePath, item.Content);
}

关键变化说明

  • 移除对旧版TfsTeamProjectCollection、VersionControlServer、Workspace类的依赖,改用VssConnection和TfvcHttpClient
  • 工作区创建通过CreateWorkspaceAsync方法完成,映射规则在参数中直接定义
  • 原Workspace.Get()的本地同步逻辑需手动实现:通过API获取文件列表,逐个下载到本地对应路径(REST API不提供客户端侧的一键同步功能)

内容的提问来源于stack exchange,提问作者Cy Ra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:32:14