TFS以编程方式通过路径命中DownloadTicketValidationException来加载文件

时间:2016-11-28 16:15:07

标签: c# tfs

概述/ TLDR

我面临的情况是,我有一个来自TFS的文件路径数据库表,需要以编程方式下载每个文件来逐行解析它们,寻找特定的代码片段。目前,我正在使用VersionControlServer.DownloadFileByUrl(filePath)来获取文件流,但我正面临着#34; DownloadTicketValidationException:TF15006:请求文件ID丢失或为空。"

我想知道:

A。)如何检索和设置此请求文件ID以便检索此文件?

B。)是否有另一种方法可以检索所述文件,只提供不会遇到此问题的网址?

详细

//Omitted wrapping try-catch block for brevity
private static string GetFileAsString(ref VersionControlServer server, string filePath)
{
    string content;
    using (var fileStream = server.DownloadFileByUrl(filePath))
    {
        using (var memoryStream = new MemoryStream())
        {
            fileStream.CopyTo(memoryStream); // Throws  DownloadTicketValidationException:TF15006: The request file ID was missing or empty.
            using (var streamReader = new StreamReader(new MemoryStream(memoryStream.ToArray())))
            {
                content = streamReader.ReadToEnd();
            }
        }
    }
    return content;
}

文件路径的格式为$"{serverURL}:8080/tfs/{collectionName}/{filepath.TrimStart('$', '/')}",它似乎是一个有效的路径(我可以在Team Foundation Explorer中验证它。)

使用以下命令打开VersionControlServer:

...
using (var teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new  Uri(collectionUrl)))
{
    teamProjectCollection.ClientCredentials = new TfsClientCredentials(); // Uses my credentials when running under VS, which should have access.
    var versionControlServer = teamProjectCollection.GetService<VersionControlServer>();
...

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

static void Main(string[] args)
        {
            string url = "http://tfscollectionurl/";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(url));
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            Item item = vcs.GetItem("$/Path/of/file");
            var filestream = item.DownloadFile();
            string content;
            using (var memoryStream = new MemoryStream())
            {
                filestream.CopyTo(memoryStream); // Throws  DownloadTicketValidationException:TF15006: The request file ID was missing or empty.
                using (var streamReader = new StreamReader(new MemoryStream(memoryStream.ToArray())))
                {
                    content = streamReader.ReadToEnd();
                }
            }
            Console.WriteLine(content);
            Console.ReadLine();
        }