我們正在將我們的.NET Rally代碼從SOAP移植到REST .NET API。到目前爲止,REST API似乎更快,更易於使用,因爲每次在Rally Workspace中工作產品自定義字段發生更改時都不會有WSDL中斷。如何使用Rally REST .NET向用戶故事添加附件
雖然我們嘗試複製上傳附件的功能,但我遇到了一件麻煩事。我們一直遵循一個非常類似的過程作爲一個在此公告概述:
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;
}
}
謝謝!這一直在擾亂我! – user1373451 2012-07-10 13:20:51