none
Unable to POST large files to WCF REST Service. RRS feed

  • Question

  • My requrirement is to Upload large files  from Client to WCF REST service. Am able to upload small size image(8.3kb) successfully. when trying to upload larger files say (16kb or more) I am receiveing an error. "The remote server returned an error :(400) Bad Request".

    In Server side Web config i used webHttpBinding.

    <webHttpBinding>
    <binding name="web" maxReceiveMessagesize="250000000" maxBufferSize="250000000" maxBufferPoolSize="250000000" closeTimeout="00:10:00" openTimeout="00:10:00"
    receiveTimeout="00:10:00" sendTimeOut="00:10:00">

    <readerQuotas maxDepth="250000000" maxStringContentLeangth="250000000" maxArraylength="250000000" maxBytesPerread="250000000"
    maxNameTableCharCount="250000000"/>
    <security mode="None" />
    </binding>
    </webHttpBinding>

    <security>
    <requestFiltering>
    <requestLimits maxAllowedContentLength="52428800" />
    </requestFiltering>
    </security>

    Iservice1.cs:

    [OperationContract]
    [WebInvoke(UriTemplate = "/UplaodImage", Method = "POST",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Imageupload GetImageUpload( Imageupload images);

    public class Imageupload 
    {
    [DataMember]
    public string imgName{get; set;}

    [DataMember]
    public byte[] img{get; set;}

    }

    In service1.cs

    public Imageupload GetImageUpload( Imageupload images)

    {

    // upload functionality
    }

    From Client: In ButtonClick event :

    byte[] newImg= File.ReadAllBytes(@"D:\temp\img.png")
    Imageupload images = new Imageupload {imgName ="img.png",img =newImg};

    WebClient Proxy1 = new WebClient();
    Proxy1.Headers["Content-type"] = "application/json";
    MemoryStream ms = new MemoryStream();
    DataContractJsonSerializer sezToUplaod = new DataContractJsonSerializer(typeof(Imageupload));

    sezToUplaod .WriteObject(ms, Imageupload);
    byte[] responceData = Proxy1.UploadData(http://localhost:XXXX/Service1.svc/UplaodImage", "POST", ms.ToArray());
    Stream stream = new MemoryStream(responceData);

    DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(Imageupload));

    Thanks in advance...
    Tuesday, August 27, 2013 10:06 AM

Answers

  • Hi,

    From your discription, I know that you have met the (400) Bad Request error, if we implement a WCF service using the default values and expect to be able to send to the server a large file, then we may encounter an intermittent bad request error from the server.

    For more information, please try to refer to:
    #WCF Service returns (400) Bad Request:
    http://www.codeproject.com/Articles/169928/WCF-Service-returns-400-Bad-Request .

    From the configure file, I see that you have increased the size to upload large data as following:

    <webHttpBinding>
     <binding name="web" maxReceiveMessagesize="250000000" maxBufferSize="250000000" maxBufferPoolSize="250000000" closeTimeout="00:10:00" openTimeout="00:10:00"
     receiveTimeout="00:10:00" sendTimeOut="00:10:00">
    
     <readerQuotas maxDepth="250000000" maxStringContentLeangth="250000000" maxArraylength="250000000" maxBytesPerread="250000000"
     maxNameTableCharCount="250000000"/>
     <security mode="None" />
     </binding>
     </webHttpBinding>

    But it still can not help. Please try to check that you have already used the binding configure in the endpoint as following:

    <endpoint address="http://"
                    binding="webHttpBinding" bindingConfiguration="web"
                    contract="..." name="..." />


    If your question still can not be solved, please try to enable the wcf tracing to find the cause.
    #How to enable the wcf tracing:
    http://msdn.microsoft.com/en-us/library/ms733025.aspx .

    Here is an example about uploading file in wcf, please try to refer to:

    #How to upload a file via WCF REST Service using webHttpBinding:
    http://boomtobim.blogspot.in/2012/04/how-to-upload-filevia-wcf-rest.html .

    Best Regards,
    Amy Peng


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.





    Wednesday, August 28, 2013 6:10 AM
    Moderator

All replies

  • Hi,

    From your discription, I know that you have met the (400) Bad Request error, if we implement a WCF service using the default values and expect to be able to send to the server a large file, then we may encounter an intermittent bad request error from the server.

    For more information, please try to refer to:
    #WCF Service returns (400) Bad Request:
    http://www.codeproject.com/Articles/169928/WCF-Service-returns-400-Bad-Request .

    From the configure file, I see that you have increased the size to upload large data as following:

    <webHttpBinding>
     <binding name="web" maxReceiveMessagesize="250000000" maxBufferSize="250000000" maxBufferPoolSize="250000000" closeTimeout="00:10:00" openTimeout="00:10:00"
     receiveTimeout="00:10:00" sendTimeOut="00:10:00">
    
     <readerQuotas maxDepth="250000000" maxStringContentLeangth="250000000" maxArraylength="250000000" maxBytesPerread="250000000"
     maxNameTableCharCount="250000000"/>
     <security mode="None" />
     </binding>
     </webHttpBinding>

    But it still can not help. Please try to check that you have already used the binding configure in the endpoint as following:

    <endpoint address="http://"
                    binding="webHttpBinding" bindingConfiguration="web"
                    contract="..." name="..." />


    If your question still can not be solved, please try to enable the wcf tracing to find the cause.
    #How to enable the wcf tracing:
    http://msdn.microsoft.com/en-us/library/ms733025.aspx .

    Here is an example about uploading file in wcf, please try to refer to:

    #How to upload a file via WCF REST Service using webHttpBinding:
    http://boomtobim.blogspot.in/2012/04/how-to-upload-filevia-wcf-rest.html .

    Best Regards,
    Amy Peng


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.





    Wednesday, August 28, 2013 6:10 AM
    Moderator
  • Hi codenet1,

    Are you hosting your WCF REST service in IIS (via ASP.NET web application)? If so, in addition to those message size and serialization reader related quota settings, you might also need to enlarge the "maxRequestLength" attribute of the <httpRuntime> config element of ASP.NET web application.

    #maxRequestLength attribute of httpRuntime Element (ASP.NET Settings Schema)
    http://msdn.microsoft.com/en-us/library/vstudio/e1f13641(v=vs.100).aspx

    and here is a web entry which also mentioned similar issue and how we can troubleshoot this problem and which configuration settings we need to adjust.

    #Increasing maximum request length for WCF REST Web Service with ASP .NET 4.0
    http://tech.avivo.si/2011/10/increase-maximum-request-length-for-wcf-rest-web-service-asp-net-4/


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 28, 2013 7:47 AM
    Moderator
  •  

    After keeping  bindingConfiguration="web" in <endpoint> it is working fine.

    Thank you Amy Peng.

    Thursday, August 29, 2013 11:01 AM