Hi,
I am trying to send an object
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)
{
throw new Exception($"Unable to retrieve document for {fileId}");
}
}