I get a stream form a web service:
Stream resultStream = WebServiceEntities.GetAttachment(attachmentId);
I need to convert this stream into a byte array but
Attempt # 1
byte[] resultBytes = null;
using (Stream stream = resultStream)
{
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (stream.CanRead && count > 0);
resultBytes = ms.ToArray();
}
}
Attempt # 2:
var memoryStream = new MemoryStream();
resultStream.CopyTo(memoryStream);
byte[] resultBytes = memoryStream.ToArray();
In both cases the (byte[] resultBytes) always returns an empty byte array, does anyone know what is causing this to happen? Is there something wrong with the Stream the web service returns?