文件附件到工作项 - TFS 2015

时间:2017-12-27 21:00:45

标签: c# tfs2015

我正在尝试使用TFS测试用例工作项附加文件。作为第一步,我正在尝试在TFS附件存储中创建附件文件。一旦在附件存储中创建了文件,我将获得AttachmentReference对象,并且使用该对象,我计划将文件附加到ID为595的所选工作项。但是,我的进程在CreateAttachmentAsync函数调用时挂起。 任何帮助表示赞赏!

 public void AttachFile(VssConnection connection)
    {
        //use the workItemTrackingHttpClient
        try
        {
            using (WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>())
            {
                //create a work item
                //WorkItem wi = witClient.GetWorkItemAsync(595, expand: WorkItemExpand.All).Result;
                string filePath = @"C:\Temp\attach-file.PNG";
                AttachmentReference attachRef = witClient.CreateAttachmentAsync(filePath, "simple").Result;

                JsonPatchDocument patchDocument = new JsonPatchDocument();

                //add fields to your patch document
                patchDocument.Add(
                    new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path = "/relations/-",
                        Value = new
                        {
                            rel = "AttachedFile",
                            url = attachRef.Url,
                            attributes = new { comment = "Adding new attachment for Test Case 2" }
                        }
                    }
                );

                WorkItem result = witClient.UpdateWorkItemAsync(patchDocument, 595).Result;
            }
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试删除AttachmentReference attachRef = witClient.CreateAttachmentAsync(filePath, "simple").Result中的“简单”。请尝试以下代码:

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using System;

namespace UploadAttachment
{
    class Program
    {
        static void Main(string[] args)
        {

            // Full path to the binary file to upload as an attachment
            string filePath = @"C:\Temp\attach-file.PNG";
            var myCredentials = new VssClientCredentials();
            var connection = new VssConnection(new Uri(@"http://tfsserver:8080/tfs/TeamProjectCollectionName"), myCredentials);
            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
            AttachmentReference attachment = workItemTrackingClient.CreateAttachmentAsync(filePath).Result;
            JsonPatchDocument patchDocument = new JsonPatchDocument();
            patchDocument.Add(
                 new JsonPatchOperation()
                 {
                     Operation = Operation.Add,
                     Path = "/relations/-",
                     Value = new
                     {
                         rel = "AttachedFile",
                         url = attachment.Url,
                         attributes = new { comment = "Adding new attachment for Test Case" }
                     }
                 }
             );

            WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, 595).Result;



        }
    }
}