I am developing a sample Xamarin.Forms app, which take a photo and after that upload it to a remote server. The upload part is handled by a WCF service deployed on remote IIS.
The client part is ok, but when the flow arrive to the server part, I receive this error:
System.Net.WebException: There was an error in processing web request: Status code 413(RequestEntityTooLarge): Request Entity Too Large
I tried to increase the maxi size of the message, both in WCF's web.config and in the site where the service is deployed.
Below the service's web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="GlobalEndpoint">
<dataContractSerializer maxItemsInObjectGraph="1365536" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="GlobalBehavior">
<dataContractSerializer maxItemsInObjectGraph="1365536" />
</behavior>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding
name="BasicHttpBinding_IQuery_service"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
transferMode="Streamed">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://path/to/service.svc"
binding="webHttpBinding" bindingConfiguration="BasicHttpBinding_IQuery_service"
contract="IQuery_service" name="BasicHttpBinding_IQuery_service" />
</client>
<protocolMapping>
<add binding="webHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
Below the settings of the Configuration editor of the site, where is deployed the WCF service:
Below the method which I call for uploading the image:
public bool UploadToFolder(byte[] file_bytes, string file_name)
{
bool isSuccess = false;
FileStream fileStream = null;
//Get the file upload path store in web services web.config file.
string strFolderPath = System.Configuration.ConfigurationManager.AppSettings.Get(@"dest\path");
try
{
if (!string.IsNullOrEmpty(strFolderPath))
{
if (!string.IsNullOrEmpty(file_name))
{
string strFileFullPath = strFolderPath + file_name;
fileStream = new FileStream(strFileFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
// write file stream into the specified file
using (System.IO.FileStream fs = fileStream)
{
fs.Write(file_bytes, 0, file_bytes.Length);
isSuccess = true;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return isSuccess;
}
I don't know if this is the best way to perform an upload to remote server from Xamarin app, any suggest is appreciated.
EDIT
Already tried to increment maxReceivedMessageSize
in web.config, without success.
Such as I read on the main page of the service, I run the command svcutil.exe http://server_address/path/to/service.svc?wdsl
, after that I added the file.cs to my solution and from this I call the varius methods.
The piece of code where I call the upload method:
try
{
Query_serviceClient client = new Query_serviceClient(http_binding, endpoint_address);
client.UploadToTempFolder(filebyte, filename);
client.Close();
}
catch (Exception ex)
{
DisplayAlert("ERROR", ex.ToString(), "OK");
}