2

Could someone clarify a piece of information I cannot put my finger on it...

According to tens of question on SO the only way to send byte[] is using OperationContract with a Stream parameter.

Why is that a wcf rest service cannot serialize a byte[] property, but if I use a string one instead and Convert.ToBase64String(bytes) everything goes well?

EDIT 1

[OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Save/")]
Response Save(Document doc);

Now for the sake of the length I won't post the whole model. There is a property in Document named File which has property of type byte[].

Now in my client I have the following:

using (FileStream file = System.IO.File.Open(@"C:\D\pic.png", FileMode.Open))
                {
model.File = new File
                        {
                            FileName = "pic.png",
                            Description = "some description",
                            FileType = new Nomenclature { Id = 6 },
                            Content = file.ToByteArray(); // this is a function copied from SO and it works..
                        }


      ...some more property are being initialized here

                    var data = JsonConvert.SerializeObject(model);
                    var content = new StringContent("{ \"doc\" : " + data + " }", Encoding.Unicode, "application/json");
                    var result =
                        client.PostAsync(new Uri("http://localhost/MyTestWCF/Save"), content)
                            .Result;
                    var tmp = result.Content.ReadAsStringAsync();
                        });
                }

This results in 400 bad request

However if I use Content of type string and Convert.ToBase64String(file.ToByteArray()) - it works perfectly

2
  • 1
    I've sent byte[] without using streams just fine. Showing your code and the error you get would help us help you.
    – Tim
    May 11, 2015 at 9:58
  • I am also having same issue.. how did you solve this... stackoverflow.com/questions/60714522/…
    – Ziggler
    Mar 18, 2020 at 16:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.