We're in the process of porting our .NET Rally code from SOAP to the REST .NET API. So far so good, the REST API seems to be faster and is easier to use since there's no WSDL to break each time the work product custom fields change in the Rally Workspace.
I'm having trouble with one thing though as we try to replicate the ability to upload attachments. We're following a very similar procedure as to the one outlined in this posting:
Rally SOAP API - How do I add an attachment to a Hierarchical Requirement
Whereby the image is read into a System.Drawing.Image. We use the ImageToByteArray function to convert the image to a byte array which then gets assigned to the AttachmentContent, which is created first.
Then, the Attachment gets created, and wired up to both AttachmentContent, and the HierarchicalRequirement.
All of the creation events work great. The content object gets created fine. Then the new attachment called "Image.png" gets created and linked to the Story. But when I download the resulting attachment from Rally, Image.png has zero bytes! I've tried this with different images, JPEG's, PNG's, etc. all with the same results.
An excerpt of the code showing our process is included below. Is there something obvious that I'm missing? Thanks in advance.
// .... 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;
}
}