Consume Restful WCF service in json RRS feed

  • Question

  • I have created an WCF Restful Service with Data Contract

    public class Employee
        public int Id { get; set; }

        public string Name { get; set; }


    public byte[] Img { get; set; }


    [WebInvoke(Method = "POST", UriTemplate = "/SaveEmployee", 
               RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    bool SaveEmployee(Employee employee);

    Client is:

               WebClient client = new WebClient();
                client.Headers["Content-type"] = "application/json";

                Employee emp = new Employee();
                emp.Id = 1;


                emp.Img = GetImageInBytes(); // its returning byte[] array

                MemoryStream stream = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Employee));
                serializer.WriteObject(stream, emp);

                byte[] data = client.UploadData("http://localhost:11523/Service1.svc/SaveEmployee", "POST", stream.ToArray());

                stream = new MemoryStream(data);
                serializer = new DataContractJsonSerializer(typeof(bool));
                bool result = (bool)serializer.ReadObject(stream);

     Now in Client while Consuming the WCF Service:
    If i don't use Img data member that is of type byte[] , then WCF Rest Service is working fine.

    But if i use byte[] as Data Member WCF Service gives me error 400 Bad Request error on UploadData. Please help to resolve the issue.



    Omendra Gangwar

    Saturday, January 12, 2013 5:04 PM


  • Hi,

    Do you check this thread on similar issue and try the solution by giving full access to related folder?

    If the issue not solved, please enable WCF tracing to get the internal error.

    Best Regards.

    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Haixia_Xie Monday, January 21, 2013 9:51 AM
    Thursday, January 17, 2013 9:07 AM