0

I know this topic has been discused multiple times already, but unfortunately non of the provided solutions workd for me. I try to transfer large files (up to 1.5 GB) from a client console application to a WCF service. But I always get an HTTP error The remote server returned an unexpected response: (413) Request Entity Too Large. while transmitting the file content.

All information I found in the internet where about adding maxContentLength and similar configuration to web.config file. But I assume I entered them at a wrong part from the web.config or so.

Edit 26.02.2020 18:35 (updated due to hints and new tests)

Based on the tipps from above I added some entries to config files and did some more tests. In the mean time I found out a few things:

  • The number in web.config define the size in bit not in bytes as I read on severall pages
  • The number must be a valid int32 - so the maximum value is 2147483647
  • 2147483647 bit are around 256 MByte - so it's understandable, that my testfile with around 400MB caused a problem

Overall, if it's not possible to transfer the large files - at least 20-30 MB should be possible. For larger files I will find an other solution then.

To do easier tests I just created a new empty WCF service and a console application to test it.

You can find the complete sourcecode on Google Drive. I included a 22MB test image as well, which doesn't work to transfer.

Different to my first problem, I now get a 404 error instead of a 413. So somehow the IIS returns a 404 when the request is not matchable to a service instead of the previous 413. A pretty strange behaviour for me.

The web.config and the app.config looks still the same as before (beside there is no entity framework stuff in).

Server web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="mybinding"  maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http" bindingConfiguration="mybinding" />
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

Client app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="mybinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:53042/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="mybinding" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

As I'm not a pro regarding web.config configuration, I assume, I just added the configuration in a wrong section of the XML. Can anybody provide me some help, how my web.config need to look like, that I can transfer larger files.

Thanks in advance

Regards Markus

Markus H.
  • 69
  • 8

1 Answers1

1

The service now publishes the endpoint using ProtocolMapping section, I suggest you name the binding and apply the configuration to the properties below.

    <bindings>
      <basicHttpBinding>
        <binding name="mybinding" ... >
        ...
        </binding>
      </basicHttpBinding>
    </bindings>
<protocolMapping>
      <add binding="basicHttpBinding" scheme="http" bindingConfiguration="mybinding"/>
</protocolMapping>

If it doesn’t work, we could publish the service by using the following style.

<system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
</services>

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

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Thanks for your hints. unfortuanately I still retrieve the same error (413). I updated the **web.config** in first post to show, how it looks in the mean time. Do I need to add anything to the clients **appconfig**, too? – Markus H. Feb 25 '20 at 07:18
  • @MarkusH. You can try it on the client. This is theoretically needed. Also, are you sure that the client configuration is now automatically generated by the server above? – Abraham Qian Feb 25 '20 at 09:28
  • I tried serverall settings on the server - at the end, the clients app.config was enhanced to version I added to first post, after updateing the service refernce. On a virtual development environment, I got out-of-memory execptions after these changes. but on my main computer I didn't - so I asume it's not a general problem. But I still retrive the error 413. _(Config files in first post updated to latest version)_ – Markus H. Feb 25 '20 at 16:49
  • yes, it might not a general problem. Please create a new console app, add service reference, then call the remote service again by using a proxy. – Abraham Qian Feb 26 '20 at 06:39
  • Something is pretty strange - I created a new client console app - now I retrieve a 404 instead. I will create a new test console client and a new test service on my main computer as soon as possible. I think something is weird within my current VS-solution. As soon as created the test environment, I will add a new answer here so we can discuss about again. Thanks so far for your inputs – Markus H. Feb 26 '20 at 09:33
  • I updated the first post containing the latest version of code and config – Markus H. Feb 26 '20 at 17:39
  • 1
    It doesn't refer to bit, but byte. Please refer to this document. It doesn't refer to bit, but byte. Please refer to this document. https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpbinding.maxreceivedmessagesize?view=netframework-4.0 – Abraham Qian Feb 27 '20 at 08:11
  • Maybe I did some tests in a wrong way so I assumed it's in Bit. But anyway: if it's in Byte I should be able to transfer around 2GB. – Markus H. Feb 27 '20 at 11:42