如何将附件添加到使用REST拉力赛.NET用户故事(How to Add an Attachment

2019-07-30 08:05发布

我们从SOAP移植我们的.NET代码拉力赛到REST .NET API的过程。 到目前为止好,在REST API似乎是更快,更容易使用,因为没有WSDL每次工作产品的自定义字段中的拉力赛工作区更改打破。

我有一个麻烦的事情,虽然我们试图复制到上传附件的能力。 我们一直遵循一个非常类似的操作程序,在此公告中列出的一个:

拉力SOAP API -如何添加附件的层次需求

从而使图像读入System.Drawing.Image对象。 我们使用ImageToByteArray功能将图像转换,然后被分配到AttachmentContent,这是第一次创建一个字节数组。

然后,附件被创建和有线长达两个AttachmentContent和HierarchicalRequirement。

所有创建活动的工作的伟大。 内容对象被创建的罚款。 那么所谓的“Image.png”新的附件被创建并链接到故事。 但是,当我从拉力下载最终的附件,Image.png具有零个字节! 我曾与不同的图像,JPEG的,巴新等,都具有相同的结果尝试这样做。

展示我们处理的代码的摘录包含下面。 有没有办法,我失去了一些东西明显? 提前致谢。

    // .... Read content into a System.Drawing.Image called imageObject ....

    // Convert Image to byte array
    byte[] imageBytes = ImageToByteArray(imageObject, System.Drawing.Imaging.ImageFormat.Png);
    var imageLength = imageBytes.Length;

    // AttachmentContent
    DynamicJsonObject attachmentContent = new DynamicJsonObject();
    attachmentContent["Content"] = imageBytes ;

    CreateResult cr = restApi.Create("AttachmentContent", myAttachmentContent);
    String contentRef = cr.Reference;
    Console.WriteLine("Created: " + contentRef);

    // Set up attachment
    DynamicJsonObject newAttachment = new DynamicJsonObject();
    newAttachment["Artifact"] = story;
    newAttachment["Content"] = attachmentContent;
    newAttachment["Name"] = "Image.png";
    newAttachment["ContentType"] = "image/png";
    newAttachment["Size"] = imageLength;
    newAttachment["User"] = user;


    // Create the attachment in Rally
    cr = restApi.Create("Attachment", newAttachment);

    String attachRef = cr.Reference;
    Console.WriteLine("Created: " + attachRef);

}

public static byte[] ImageToByteArray(Image image, System.Drawing.Imaging.ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        image.Save(ms, format);

        // Convert Image to byte[]                
        byte[] imageBytes = ms.ToArray();
        return imageBytes;
    }
}

Answer 1:

这个问题也让我困惑了一会儿 - 终于整理出来大约一个星期前。

两种意见:

  1. 虽然拉力赛的SOAP API将序列化的字节数组到幕后Base64编码字符串,REST没有做到这一步你并期待一个Base64格式的字符串作为AttachmentContent对象的内容属性进行传递。
  2. 在您的例子所示System.Drawing.Image.Length不会提供拉力赛是WSAPI期待的正确长度。 你需要通过Base64编码格式的字符串的长度被转换回常规字符串之后。 这也是一样的字节数组的长度。

我包括一个代码示例来说明:

// System Libraries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Web;

// Rally REST API Libraries
using Rally.RestApi;
using Rally.RestApi.Response;

namespace RestExample_CreateAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set user parameters
            String userName = "user@company.com";
            String userPassword = "password";

            // Set Rally parameters
            String rallyURL = "https://rally1.rallydev.com";
            String rallyWSAPIVersion = "1.36";

            //Initialize the REST API
            RallyRestApi restApi;
            restApi = new RallyRestApi(userName,
                                       userPassword,
                                       rallyURL,
                                       rallyWSAPIVersion);

            // Create Request for User
            Request userRequest = new Request("user");
            userRequest.Fetch = new List<string>()
                {
                    "UserName",
                    "Subscription",
                    "DisplayName",
                };

            // Add a Query to the Request
            userRequest.Query = new Query("UserName",Query.Operator.Equals,userName);

            // Query Rally
            QueryResult queryUserResults = restApi.Query(userRequest);

            // Grab resulting User object and Ref
            DynamicJsonObject myUser = new DynamicJsonObject();
            myUser = queryUserResults.Results.First();
            String myUserRef = myUser["_ref"];

            //Set our Workspace and Project scopings
            String workspaceRef = "/workspace/12345678910";
            String projectRef = "/project/12345678911";
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            // Find User Story that we want to add attachment to

            // Tee up Story Request
            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeDown = projectScopingDown;
            storyRequest.ProjectScopeUp = projectScopingUp;

            // Fields to Fetch
            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "FormattedID"
                };

            // Add a query
            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US43");

            // Query Rally for the Story
            QueryResult queryResult = restApi.Query(storyRequest);

            // Pull reference off of Story fetch
            var storyObject = queryResult.Results.First();
            String storyReference = storyObject["_ref"];

            // Read In Image Content
            String imageFilePath = "C:\\Users\\username\\";
            String imageFileName = "image1.png";
            String fullImageFile = imageFilePath + imageFileName;
            Image myImage = Image.FromFile(fullImageFile);

            // Get length from Image.Length attribute - don't use this in REST though
            // REST expects the length of the image in number of bytes as converted to a byte array
            var imageFileLength = new FileInfo(fullImageFile).Length;
            Console.WriteLine("Image File Length from System.Drawing.Image: " + imageFileLength);

            // Convert Image to Base64 format
            string imageBase64String = ImageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png);           

            // Length calculated from Base64String converted back
            var imageNumberBytes = Convert.FromBase64String(imageBase64String).Length;

            // This differs from just the Length of the Base 64 String!
            Console.WriteLine("Image File Length from Convert.FromBase64String: " + imageNumberBytes);

            // DynamicJSONObject for AttachmentContent
            DynamicJsonObject myAttachmentContent = new DynamicJsonObject();
            myAttachmentContent["Content"] = imageBase64String;

            try
            {
                CreateResult myAttachmentContentCreateResult = restApi.Create("AttachmentContent", myAttachmentContent);
                String myAttachmentContentRef = myAttachmentContentCreateResult.Reference;
                Console.WriteLine("Created: " + myAttachmentContentRef);

                // DynamicJSONObject for Attachment Container
                DynamicJsonObject myAttachment = new DynamicJsonObject();
                myAttachment["Artifact"] = storyReference;
                myAttachment["Content"] = myAttachmentContentRef;
                myAttachment["Name"] = "AttachmentFromREST.png";
                myAttachment["Description"] = "Attachment Desc";
                myAttachment["ContentType"] = "image/png";
                myAttachment["Size"] = imageNumberBytes;
                myAttachment["User"] = myUserRef;

                CreateResult myAttachmentCreateResult = restApi.Create("Attachment", myAttachment);

                List<string> createErrors = myAttachmentContentCreateResult.Errors;
                for (int i = 0; i < createErrors.Count; i++)
                {
                    Console.WriteLine(createErrors[i]);
                }

                String myAttachmentRef = myAttachmentCreateResult.Reference;
                Console.WriteLine("Created: " + myAttachmentRef);

            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled exception occurred: " + e.StackTrace);
                Console.WriteLine(e.Message);
            }
        }

        // Converts image to Base 64 Encoded string
        public static string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                // Convert Image to byte[]                
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);

                return base64String;
            }
        }
    }
}


文章来源: How to Add an Attachment to a User Story using Rally REST .NET
标签: c# .net rest rally