none
How to upload an Image to WCF Service

    Question

  • I am trying to upload an image to a WCF Service. I have converted my image into bytes and passing array of bytes to WCF Service.

     

    But it's throwing an exception "The remote server returned an unexpected response: (400) Bad Request".Let me know if any one require further clarifiaction.

    Do I have to use any specific protocol for it?

    Thanks

    Kanhaiya

    Friday, January 19, 2007 10:02 AM

Answers

  • The problem is probably the message size. You have to increase the message size and byte array length for the service and client:

     

    <system.serviceModel>

    <services>

    <service name="FileUploadService.FileUpload"

    <endpoint address="http://localhost:8080/FileUpload" contract="FileUploadService.IFileUpload" binding="wsHttpBinding" bindingConfiguration="wsHttp" listenUri="http://localhost:8000/FileUpload"/>

    </service>

    </services>

    <bindings>

    <wsHttpBinding>

    <binding name="wsHttp" maxReceivedMessageSize ="50000000" messageEncoding="Mtom" maxBufferPoolSize="50000000" >

    <readerQuotas maxArrayLength="50000000" />

    <security mode="None" />

    </binding>

    </wsHttpBinding>

    </bindings>

    </system.serviceModel>

    Friday, January 19, 2007 5:07 PM
    Moderator

All replies

  • The problem is probably the message size. You have to increase the message size and byte array length for the service and client:

     

    <system.serviceModel>

    <services>

    <service name="FileUploadService.FileUpload"

    <endpoint address="http://localhost:8080/FileUpload" contract="FileUploadService.IFileUpload" binding="wsHttpBinding" bindingConfiguration="wsHttp" listenUri="http://localhost:8000/FileUpload"/>

    </service>

    </services>

    <bindings>

    <wsHttpBinding>

    <binding name="wsHttp" maxReceivedMessageSize ="50000000" messageEncoding="Mtom" maxBufferPoolSize="50000000" >

    <readerQuotas maxArrayLength="50000000" />

    <security mode="None" />

    </binding>

    </wsHttpBinding>

    </bindings>

    </system.serviceModel>

    Friday, January 19, 2007 5:07 PM
    Moderator
  • If you prefer can also to use the Serializable Image and Bitmap objects and send it. Only you need to specify that types as Known Types, using the KnownTypeAttribute attribute in the implementation class and ServiceKnownTypeAttribute attribute in the service contract interface.
    Friday, January 19, 2007 6:05 PM
  • I am experiencing the same problem and your post is the solution to it.

     

    But: how can I set those properties programmatically?

     

    basicHttpBinding.maxReceivedMessageSize doesn't exist (or I failed to find it).

     

    Thank you.

    Tuesday, June 05, 2007 12:21 PM
  • We had same problem but decided to break file in chunks and upload chunck by chunck instead increasing maxmessage size. Tih solution will always work, since size of files uploaded is unpredictable.
    Wednesday, December 05, 2007 10:33 AM
  • Thank you! The missing part in my web.config puzzle was:

     

    <binding>

        <wsHttpBinding>

    <binding name="wsHttp"

                 maxReceivedMessageSize ="50000000"

                 messageEncoding="Mtom"

                 maxBufferPoolSize="50000000" />

    <readerQuotas maxDepth="32"

       maxStringContentLength="2147483647" 

       maxArrayLength="2147483647"

                 maxBytesPerRead="8192"

       maxNameTableCharCount="2147483647" />

       </wsHttpBinding>

    </binding>

    Friday, May 09, 2008 6:25 PM
  • It was the image size - you were right.
    But changing those size properties didn't work.I can send and receive small byte[] , but not larger.
    Namely:
                         maxBufferPoolSize="52428800000"
                         maxReceivedMessageSize="655360000000"

    With these settings, I am able to send an image of 13.6KB, but not one of 35.9KB.
    I don't understand why, but I'll eventually find a solution - even if I have to break it in chunks, as said.
    (Though that's more of a hack. ; )
    Tuesday, July 15, 2008 4:51 PM
  • I am actually doing the same thing you are doing (in regards to transmitting images). In addition to the settings mentioned above, another setting I had to change was the MaxArrayLength property of the ReaderQuota. I am still searching for the app.config way to accomplish this (which is how I ended up here), but in code, it would be as follows:

    Code Snippet
    I set my buffer size to 1 MB

    BasicHttpBinding bhtp = new BasicHttpBinding();

    bhtp.MaxReceivedMessageSize = SERVICE_BUFFER_SIZE;
    bhtp.MaxBufferSize = SERVICE_BUFFER_SIZE;
    bhtp.ReaderQuotas.MaxArrayLength = SERVICE_BUFFER_SIZE;
    bhtp.ReaderQuotas.MaxBytesPerRead = SERVICE_BUFFER_SIZE;

    host.AddServiceEndpoint(type, bhtp, "ImageService");


    Once I changed the value on the MaxArrayLength, I was able to view the image on the server. I am not sure what the MaxBytesPerRead is doing, but I am pretty sure I got an error before I set it to the same size, so I set it to the same size as the buffer size to be safe.

    When I find the app.config way to do this, I will post again.

    I hope this helps!

    I guess after taking a second glance at the prior posts to this thread, the app.config solution is already posted Smile

    I can't see why the size changes didn't work for your larger images, because they work for me.
    Thursday, August 21, 2008 1:48 PM
  • I believe I've tried everything suggested and I still have the same issue. 

    On the service I have:
    <bindings>
        <wsHttpBinding>
            <binding name="wsHttp" maxReceivedMessageSize ="50000000" messageEncoding="Mtom" maxBufferPoolSize="50000000" >
                <readerQuotas maxArrayLength="50000000" />
                <security mode="None" />
            </binding>
        </wsHttpBinding>
    </bindings>

    On the client I have:
    <bindings>
       <wsHttpBinding>
        <binding name="WSHttpBinding_IDataManipulationsService" closeTimeout="00:01:00"
         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
         bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
         maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
         messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
         allowCookies="false">
         <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
          maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
         <reliableSession ordered="true" inactivityTimeout="00:10:00"
          enabled="false" />
         <security mode="Message">
          <transport clientCredentialType="Windows" proxyCredentialType="None"
           realm="" />
          <message clientCredentialType="Windows" negotiateServiceCredential="true"
           algorithmSuite="Default" establishSecurityContext="true" />
         </security>
        </binding>
       </wsHttpBinding>
      </bindings>

    So as far as I can tell, I'm increasing the max sizes on both the service and client, but to no avail.
    Friday, June 12, 2009 3:56 PM
    • Proposed as answer by faycel Thursday, October 01, 2009 2:08 PM
    Wednesday, August 05, 2009 6:43 AM
  • Check this post.
    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/b4c81e92-431b-4f90-8304-ed3c8dd6c6ca

    Please how can I receive the image (file) from the the POST Request using WCF,
    Thursday, October 01, 2009 2:09 PM
  • has sameone an answer
    Thursday, October 01, 2009 2:11 PM
  • Take a look at the post at http://blogs.msdn.com/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx for information on how to implement an image upload service.
    Thursday, October 01, 2009 5:10 PM