【问题标题】:How to Add an Attachment to a User Story using Rally REST .NET如何使用 Rally REST .NET 向用户故事添加附件
【发布时间】:2012-07-09 03:04:28
【问题描述】:

我们正在将 .NET Rally 代码从 SOAP 移植到 REST .NET API。到目前为止一切都很好,REST API 似乎更快并且更易于使用,因为每次 Rally 工作区中的工作产品自定义字段发生更改时都不会中断 WSDL。

在我们尝试复制上传附件的功能时,我遇到了一件事。我们遵循与本文中概述的非常相似的程序:

Rally SOAP API - How do I add an attachment to a Hierarchical Requirement

图像由此被读入 System.Drawing.Image。我们使用 ImageToByteArray 函数将图像转换为字节数组,然后分配给首先创建的 AttachmentContent。

然后,附件被创建,并连接到 AttachmentContent 和 HierarchicalRequirement。

所有的创作活动都很好。内容对象被创建得很好。然后创建名为“Image.png”的新附件并将其链接到故事。但是当我从 Rally 下载生成的附件时,Image.png 的字节数为零!我已经尝试过使用不同的图像、JPEG、PNG 等,结果都相同。

下面是显示我们流程的代码摘录。我有什么明显的遗漏吗?提前致谢。

    // .... 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;
    }
}

【问题讨论】:

    标签: c# .net rest rally


    【解决方案1】:

    这个问题也让我困惑了一段时间——终于在大约一周前解决了。

    两个观察结果:

    1. 虽然 Rally 的 SOAP API 会在后台将字节数组序列化为 Base64 字符串,但 REST 不会为您执行此步骤,并且希望将 Base64 格式的字符串作为 AttachmentContent 对象的 Content 属性传递。
    2. 您的示例中显示的 System.Drawing.Image.Length 不会提供 Rally 的 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;
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢!这一直困扰着我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    相关资源
    最近更新 更多