Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to send an object
C#
public class FileStreamDto
    {
        public FileStreamDto(){}}
        public string Name { get; set; }
        public Stream Data { get; set; }
    }

from Service layer in my case Asp.net Web API2 to a client controller. I can successfully send a stream Data to the client controller but i am looking to send a complex object across so that i can reconstruct the object and use at other end(Client end).

What I have tried:

I have tried
DataContractJsonSerializer
to serialize the object but i am getting following error.
Type 'System.Net.Http.WinHttpResponseStream' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.


public async Task<IActionResult> Download([FromQuery]Guid fileId)
        {
            try
            {
                var downloadResponse = await _documentProxy?.GetFile(fileId);
                var type = downloadResponse.Body.GetType();
                DataContractJsonSerializer dcj = new DataContractJsonSerializer(new FileStreamDto().GetType());
                var ms = new MemoryStream();
                dcj.WriteObject(ms, downloadResponse.Body.Data);
                var response = File(ms, "application/octet-stream", downloadResponse.Body.Name);
                return response;
            }
            catch (Exception ex)
            {
                //log and send a custom error to service
                throw new Exception($"Unable to retrieve document for {fileId}");
            }
        }
Posted
Updated 3-Apr-18 10:22am
v2
Comments
F-ES Sitecore 4-Apr-18 4:54am    
As the error says, you can't serialise a stream. I assume you are calling this method over http, so anything you return has to be able to be represented as text. A stream can contain binary data and it can also be on an volatile length (ie it might be a stream on a socket that is constantly receiving data). You'll probably have to convert your stream to base64 and pass it that way rather than as a stream, and the calling code will then have to convert it back to binary.
Moonwarrior 4-Apr-18 9:14am    
But don't you think Base64 encode string will make my data really big in size when sending over the wire as compare to sending stream
F-ES Sitecore 4-Apr-18 9:20am    
Yes, but you can't serialise a stream so you don't have much option. You could maybe provide the stream alone via a different method call so the client would get your object then download the stream but that might make your code much more complicated.
Moonwarrior 4-Apr-18 13:40pm    
Yes, I am doing latter option right now. Thanks for your input, i really appreciate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900