I have googled my *** off and cannot find a reason why this is happening. I have a WCF service (file) where I upload a PDF that is only in a memorystream. The memorystream shows I have data before it is sent to the service. When it gets to the service the stream is empty. I have a MessageContract for the upload that has a MessageBodyMember of Stream. I call the upload in the service and pass the parameters (the same ones that are in the MessageContract. When I look at the upload request object I can see all the values except Stream.
Here is my UploadRequest class:
[MessageContract]
public class UploadRequest
{
[MessageHeader(MustUnderstand = true)]
public long EntityID { get; set; }
[MessageHeader(MustUnderstand = true)]
public string FileName { get; set; }
[MessageHeader(MustUnderstand = false)]
public bool IsPrivileged { get; set; }
[MessageHeader(MustUnderstand = false)]
public string Folder { get; set; }
[MessageHeader(MustUnderstand = false)]
public Guid stream_id { get; set; }
[MessageBodyMember(Order = 1)]
public Stream Stream { get; set; }
}
Here is my call to the upload method in the service:
private void SaveAs()
{
var UserID = Convert.ToInt64(Application.Current.Properties["VetID"]);
var ms = new MemoryStream();
Control.SaveDocument(ms);
ms.Position = 0;
if (!svc.Upload(UserID, null, null, true, gVar.stream_id, ms, out string errormsg))
_ = errormsg;
}
Note: I did set the MemoryStream position to 0 and the object ms has data.
Here is FileService upload method:
public UploadResponse Upload(UploadRequest request)
{
DB.dbFileMgr fm = new DB.dbFileMgr();
return fm.UploadFile(request);
}
And here is the UploadFile method:
if (request.stream_id != null && request.stream_id != new Guid())
{
var stream = SharedCommon.ConvertStreamToMemoryStream(request.Stream);
dbConn.UpdateDocument(request.EntityID, request.IsPrivileged, request.stream_id, stream);
return new UploadResponse { UploadSucceeded = true };
}
else
{
dbConn.CreateDocumentFolder(request.Folder, request.EntityID);
var stream = SharedCommon.ConvertStreamToMemoryStream(request.Stream);
dbConn.InsertDocument(request.EntityID, stream, request.FileName, request.Folder, request.IsPrivileged);
return new UploadResponse { UploadSucceeded = true };
}
Here is what the Stream is when I put a breakpoint at the Upload: Debug info of the Stream
I have a method to convert a Stream to a MemoryStream, thinking that would help, but when it reaches the while loop in the ReadFully method it skips over it. That tells me the Stream is empty.
Here is the code for the Convert and ReadFully methods:
public static MemoryStream ConvertStreamToMemoryStream(Stream stream)
{
MemoryStream memoryStream = new MemoryStream();
if (stream != null)
{
byte[] buffer = ReadFully(stream);
if (buffer != null)
{
var binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(buffer);
}
}
return memoryStream;
}
public static byte[] ReadFully(this Stream input)
{
byte[] buffer = new byte[48 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
I have read all the Microsoft, C-SharpCorner & StackOverflow articles and I am at a loss as to what is wrong. I would appreciate any ideas as to what is going on and how to fix this.
Edit Maybe I did not make this clear, I am sorry. I can upload a file from the file system using the FileUpload.InputStream but when I try and send a dynamically created PDF that is only in a MemoryStream to the same service as the File Upload it fails. Before anyone says that the file might be bad, I can save the MemoryStream to my desktop and the file opens fine in Adobe. The issue seems to be sending a MemoryStream through MessageContract. It seems to have no problem with an InputStream (FileUpload) but not a MemoryStream.
