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.