David Larrabee
David Larrabee

Reputation: 384

WCF File Streaming 400 Bad Request

So I have reviewed a large number of the same issue being reported, and for some reason I cannot get a working version on my end, even using the existing solutions to these questions. Could someone shed some light on what I am overlooking? It almost has to be related to the file upload size problem above 64k as I see the error almost instantly as soon as I attempt to pass a file above that size.

Here is the web.config of my WCF Service

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <sessionState timeout="60" />
    <httpRuntime maxRequestLength="2097151" useFullyQualifiedRedirectUrl="true" executionTimeout="14400"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <bindings>
      <basicHttpBinding>
        <binding name="FileManager"  maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="FileManagerBehavior" name="PrimeWebServices.FileManager">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="FileManager" contract="PrimeWebServices.IFileManager"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="FileManagerBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500" maxConcurrentInstances="500"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

and here is my WCF Service code behind

namespace PrimeWebServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FileManager" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class FileManager : IFileManager
    {
        public FileManager()
        {
            HttpContext httpContext = HttpContext.Current;

            if (httpContext != null)
            {
                httpContext.Response.BufferOutput = false;
            }
        }

        public string UploadStream(Stream stream)
        {
            ...
        }
    }
}

Finally here is the client configuration settings (its a winforms client, using a service reference)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IFileManager" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1116/FileManager.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IFileManager" contract="PrimeWebServices.IFileManager"
                name="BasicHttpBinding_IFileManager" />
        </client>
    </system.serviceModel>
</configuration>

application functions fine as long as the file is under the 64kB limit I see others reporting, which makes me believe that I am just not wiring something up correctly and its failing back to a default configuration setting.

Upvotes: 0

Views: 1908

Answers (3)

ozdemiray
ozdemiray

Reputation: 71

If you use ASP.NET development server, then Streaming mode is not supported. You need to deploy the service to IIS or WCF Service application to use Streaming transfer mode.

Upvotes: 1

mthierba
mthierba

Reputation: 5667

If you're using IIS, and if you have the Request Filtering module installed, there is a maxAllowedContentLength limit for requests which defaults to 28.6MB. That's what you also need to adjust.

Sample config (setting the limit to 150MB):

<configuration>
   <system.webServer>
      <security>
         <requestFiltering>
            <requestLimits maxAllowedContentLength="157286400" />
         </requestFiltering>
      </security>
   </system.webServer>
</configuration>

See here for more information: http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits

Upvotes: 1

bryanjonker
bryanjonker

Reputation: 3416

In the client settings, make sure the BasicHttpBinding_IFileManager matches the FileManager bindings in the service.

Upvotes: 0

Related Questions