none
Consume Restful WCF service in json RRS feed

  • Question

  • I have created an WCF Restful Service with Data Contract

    [DataContract]
    public class Employee
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string Name { get; set; }

    [DataMember]

    public byte[] Img { get; set; }


    }

    [OperationContract]
    [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.Name="john";

                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.

    Thanks

    Omendra

    Omendra Gangwar

    Saturday, January 12, 2013 5:04 PM

Answers