1

In my self-hosted web service I have a server method to receive an uploaded image:

[System.ServiceModel.OperationContract, WebInvoke(UriTemplate = "MalaDireta/saveImage")]
string MD_saveImage(Stream arq);

public string MD_saveImage(Stream img) {
        try {
            Image i = Image.FromStream(img);

            //Here I just show received image in a PictureBox
            new ImageTest(i).ShowDialog();
        }
        catch (Exception ex) {
            MessageBox.Show("Exception:\n" + ex.ToString());
        }
        return "test";
}

With following App.config file:

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
</bindings>
<services>
  <service name="WebService.RestService" behaviorConfiguration="Default">
    <host>
      <baseAddresses>
      </baseAddresses>
    </host>
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="WebService.ICarga"></endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="webBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

And the following client method to upload the image (client has no App.config):

    Image img = Image.FromFile("fileName");
    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Png);

    Uri uri = new Uri("http://m.y.i.p:port/MalaDireta/saveImage");

    WebClient client = new WebClient();
    ms.Position = 0;
    try {
        client.UploadData(uri, ms.ToArray());
    }
    catch (Exception exc) {
        MessageBox.Show("Error at insertion.\n Exception: " + exc.ToString());
    }

And this works perfectly as long as image size is no bigger than 60k. Otherwise, I receive "Error 400: Bad Request", in which case the server method do not even start (I've tried with a MessageBox at the start of the method). I've tried to change some things in server App.Config with no luck (probably due to my lack of understanding about the config file). Can anyone please tell me how to be able to upload larger files?

Thanks in advance.

jRicardo
  • 804
  • 10
  • 20
  • This should do the trick this example is wcf but there has to be a simliar binding for what you are trying to accomplish. http://stackoverflow.com/questions/884235/wcf-how-to-increase-message-size-quota – vikingben Feb 24 '14 at 19:20
  • Thanks, @vikingben. But still gives me same error. – jRicardo Feb 24 '14 at 19:29
  • Another thing I'll throw out there. I had a similar issue that I was fighting on a wcf webservice uploading images and it was a week before I found the error was not on the web service end but I had to change the config on the client app. Maybe worth a shot. I used the same for max message in the app.config on the binding. Good luck. – vikingben Feb 24 '14 at 19:52
  • Thanks again, @vikingben. Do you know where I can find an example of a client config file? Never used one before, and the one in my server is just a slightly modified example I found. – jRicardo Feb 24 '14 at 19:58
  • @vikingben I found an example of client App.config. It uses the server address at endpoint tag. But I have to define server address at webclient.uploadData method. How would webclient "use" App.config? – jRicardo Feb 24 '14 at 20:46
  • If you right click your client project and add a service reference there you should click advance which will allow you to add a web reference this will create the binding in your app.config. Once there you can reference that object in your c# code. Whatever you named that reference you should be able to use to call your service instead of the web client. – vikingben Feb 24 '14 at 21:23
  • @vikingben, unfortunately, I'm somehow not able to add my webservice as a reference. It do work on browsers, but it always says Error 404 or Error 405 in Visual Studio... – jRicardo Feb 25 '14 at 12:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48380/discussion-between-vikingben-and-user1531978) – vikingben Feb 25 '14 at 15:24

1 Answers1

0

You can configure max receive message size in your config file. For more information use following link.

http://msdn.microsoft.com/en-us/library/ms731361.aspx

Omer Faruk Zorlu
  • 371
  • 1
  • 18
  • Thanks for the answer. But I already tried changing to maxReceivedMessageSize="2000000000" maxBufferSize="2000000000" maxBufferPoolSize="2000000000" and still no luck, giving me same "Error 400: Bad Request" – jRicardo Feb 24 '14 at 19:28