1

I have a model for using as a parameter of the post method.

My model is:

public class SendObject
{
    public string Id { get; set; }
    public string Body { get; set; }
    public string Subject { get; set; }
    public SendObjectParameters Parameters { get; set; }
    public object[] Attachments { get; set; }
}

public class SendObjectParameters
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Address { get; set; }
}

I have an attachment that I want to send with Attachments = new[] { attachment } My attachment is an excel file and the type of attachment is FileStreamResult.

In the post method, I try to serialize this object like string content = JsonConvert.SerializeObject(sendContent);. And I get an error right there about serializing : Newtonsoft.Json.JsonSerializationException: Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'. ---> System.InvalidOperationException: Timeouts are not supported on this stream.

How can I fix this?

mordeby
  • 67
  • 6

2 Answers2

1

Short answer, you cant serilize Streams.

To achieve that, you may consider to convert your stream data to a binary object : BLOB https://en.wikipedia.org/wiki/Binary_large_object .

Than you can save your blob as a string in your json.

See: How to convert an Stream into a byte[] in C#?

Note: I do not suggest this way for any file action. You can use streams.

Cotur
  • 450
  • 2
  • 10
1

The SerializeObject-Method tries to serialize your object of type FileStreamResult. I would guess this is hard to impossible because this may contain many (native) dependencies.

I suggest converting your FileStream to a byte-array first (with the ".ToArray()"-Method) or maybe even better directly convert it to a BASE64-String, so you would be sure what exactly is serialized.

Michael Rall
  • 138
  • 8