locked
how to use byte[] byte array as Data Member in POST JSON WCF Restful Service getting error 400 Bad Request Error. RRS feed

  • Question

  • Hi,

    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

    Jaspreet Singh

    Sunday, August 5, 2012 10:02 AM

Answers

  • Hi,

    I have found the solution:

    WCF Service was not having permission to folder.

    So i have right clicked and in Security Settings of Folder , i have given full access, read write access too. Issue is resolved now.

    http://forums.asp.net/t/1704828.aspx/2/10

    will be helpful to someone else too.

    Thanks

    Jaspreet Singh

    • Marked as answer by LeoTang Saturday, August 11, 2012 9:30 AM
    Monday, August 6, 2012 4:03 PM

All replies

  • Sunday, August 5, 2012 3:41 PM
  • Hi Damir,

    Thanks for pointing me in right direction.

    In the SaveEmployee i need to store his image in root directoy of application so i  method if i use:

    File.WriteAllBytes(HostingEnvironment.MapPath("~") + "\\tmp\\"+ fileNewName, fileInfo.Content);

    If i use HostingEnvironment.MapPath then i am getting the

    The remote server returned an error: (400) Bad Request.

    if i use hardcoded path instead like below:

    File.WriteAllBytes(@"C:\avicut_out\"+ fileNewName, fileInfo.Content);

    then it's working fine.

    But i need to write image in root directory of application.

    Please help.

    Thanks

    Jaspreet Singh

    Monday, August 6, 2012 3:25 PM
  • Hi,

    I have found the solution:

    WCF Service was not having permission to folder.

    So i have right clicked and in Security Settings of Folder , i have given full access, read write access too. Issue is resolved now.

    http://forums.asp.net/t/1704828.aspx/2/10

    will be helpful to someone else too.

    Thanks

    Jaspreet Singh

    • Marked as answer by LeoTang Saturday, August 11, 2012 9:30 AM
    Monday, August 6, 2012 4:03 PM
  • Hi Jaspreet ,<o:p></o:p>

    I'm a bit supprised about „(400) Bad Request.

    Why don't you attach to service process and take a look on exception which you really get by trying to write into the file. I assume your problem is that the process which hosts your service has no permission to the root folder of application.
    If you use impersonation in the service the caller of service operation will need to have write permission to the folder.

    Please don't forget to close the case if is all solved.

    Damir


    Damir Dobric
    developers.de
    daenet.de
    daenet.eu
    daenet.com

    Monday, August 6, 2012 4:21 PM