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

    • Moved by Max Meng Monday, January 14, 2013 2:24 AM
    Saturday, January 12, 2013 10:28 AM


All replies