none
Unable to upload an image RRS feed

  • Question

  • 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.

     tried to increase the max size of the message and of the buffer, 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.




    Thursday, January 9, 2020 4:21 PM

All replies

  • Hi,

    Before answering the question, we should figure out the way you call the WCF service. This determined which way you want to call the service, then apply the configuration on the right service(Soap web service or Rest style).
    According to the above service configuration, it is invalid to host the service in IIS, namely, the WCF service is running improperly, not to mention that the error occurred in the invocation. If we tried to access the service WSDL, we will see the error details.
    Are you using a client proxy to upload the image, or directly instantiating an HttpClient class to create a post request to the service endpoint address?
    If you chose the first way to upload the image, please refer to the below configuration.

    <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
          <basicHttpBinding>
            <binding
              name="mybinding"
              maxReceivedMessageSize="2147483647">
            </binding>
          </basicHttpBinding>
        </bindings>
        <protocolMapping>
          <add binding="basicHttpBinding" scheme="http" bindingConfiguration="mybinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>


    If you want to create a Restful style WCF service, i.e., the second choice, please refer to the below configuration.

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <bindings>
          <webHttpBinding>
            <binding name="rest" maxReceivedMessageSize="2147483647">
            </binding>
          </webHttpBinding>
        </bindings>
        <protocolMapping>
          <add binding="webHttpBinding" scheme="http" bindingConfiguration="rest"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Feel free to let me know if the problem still exists.

    Best Regards

    Abraham

    Friday, January 10, 2020 9:50 AM
    Moderator