title: WCF WebServiceHostFactory MaxReceivedMessageSize configuration
body: <p>I have a <strong>RESTful WCF web service</strong> called &quot;Palladium&quot; as a project in my VS2008 solution.  It is hosted in an ASP.Net 3.5 Web Application using the <strong>WebServiceHostFactory</strong> implementation via a page called &quot;Palladium.svc&quot;.</p>
  <p>My service works in a manner similar to the one explained <a href="https://web.archive.org/web/20151112002531/http://blogs.msdn.com:80/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx" rel="nofollow noreferrer">here</a>, whereby POSTs can be received by the service along with other parameters as defined in the URITemplate.</p>
  <p>The service works well, and I can receive posted information and work with it.<br />
  My problem occurs when the post data exceeds 65k and I get the following error (obtained using <code>system.diagnostics</code> in the web.config and Microsoft Service Trace Viewer).</p>
  <blockquote>
  <p>The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.</p>
  </blockquote>
  <p>Because the service is hosted via the WebServiceHostFactory implementation, the service has default bindings set up for it by the factory.  I attempted to override these bindings by providing binding settings and endpoints in the web.config file.  When I did this however, I got an error message saying:</p>
  <blockquote>
  <p>System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</p>
  <p>For request in operation LogStartingDetails to be a stream the operation must have a single parameter whose type is Stream.</p>
  </blockquote>
  <p><code>LogStartingDetails</code> is the method I'm calling in my RESTful service.</p>
  <p>Obviously the <code>LogStartingDetails</code> method doesn't need to have a single parameter whose type is <code>Stream</code>, as when the factory was creating the bindings for me the service was responding well (or more to the point, it <em>didn't</em> need to have a single parameter when the factory was doing the work for me).</p>
  <p>After much research and hitting several brick walls, I decided to create my own class that inherits from <code>WebServiceHostFactory</code> and overrides some of the implementation in order to specify the <strong>MaxReceivedMessageSize</strong> property on the binding.<br />
  When I step through the service creation in my factory class via debug, I can see the transport receiving the new <strong>MaxReceivedMessageSize</strong> and <strong>MaxBufferSize</strong> values, but they don't appear to do anything and I still end up getting the same <code>The maximum message size quota for incoming messages (65536) has been exceeded.</code> exception thrown.</p>
  <p>Below are examples of my service code.  If someone could help me figure out what I'm doing wrong here, it would be much appreciated.</p>
  <p><strong>Palladium.svc</strong> (hosted in the ASP.Net Web Application)</p>
  <pre><code>&lt;%@ ServiceHost Language=&quot;C#&quot; Debug=&quot;true&quot; Service=&quot;CDS.PalladiumService.Palladium&quot; Factory=&quot;CDS.PalladiumService.MyWebServiceHostFactory&quot; %&gt;
  </code></pre>
  <p><strong>MyWebServiceHostFactory.cs</strong> (in the CDS.PalladiumService project)</p>
  <pre><code>using System;
  using System.ServiceModel;
  using System.ServiceModel.Activation;
  using System.ServiceModel.Channels;
  using System.ServiceModel.Web;
  
  namespace CDS.PalladiumService
  {
      public class MyServiceHost : WebServiceHost
      {
          public MyServiceHost()
          {
          }
  
          public MyServiceHost(object singletonInstance, params Uri[] baseAddresses)
              : base(singletonInstance, baseAddresses)
          {
          }
  
          public MyServiceHost(Type serviceType, params Uri[] baseAddresses)
              : base(serviceType, baseAddresses)
          {
          }
  
  
          protected override void OnOpening()
          {
              base.OnOpening();
  
              if (base.Description != null)
              {
                  foreach (var endpoint in base.Description.Endpoints)
                  {
                      var transport = endpoint.Binding.CreateBindingElements().Find&lt;TransportBindingElement&gt;();
                      if (transport != null)
                      {
                          transport.MaxReceivedMessageSize = 5242880;
                          transport.MaxBufferPoolSize = 5242880;
                      }
                  }
              }
          }
      }
      
      class MyWebServiceHostFactory : WebServiceHostFactory
      {
          protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
          {
              return new MyServiceHost(serviceType, baseAddresses);
          }
      }
  }
  </code></pre>
  <p><strong>IPalladium.cs</strong> (in the CDS.PalladiumService project)</p>
  <pre><code>using System.IO;
  using System.ServiceModel;
  using System.ServiceModel.Web;
  
  namespace CDS.PalladiumService
  {
      // NOTE: If you change the interface name &quot;IPalladium&quot; here, you must also update the reference to &quot;IPalladium&quot; in Web.config.
      [ServiceContract]
      public interface IPalladium
      {
          [OperationContract]
          [WebInvoke(Method = &quot;POST&quot;, UriTemplate = UriTemplate.LogStartingDetails)]
          void LogStartingDetails(string truckId, string palladiumId, Stream postData);
      }
  }
  </code></pre>
  <p><strong>Palladium.cs</strong> (in the CDS.PalladiumService project)</p>
  <pre><code>using System.IO;
  using System.ServiceModel.Activation;
  
  namespace CDS.PalladiumService
  {
      // NOTE: If you change the class name &quot;Palladium&quot; here, you must also update the reference to &quot;Palladium&quot; in Web.config.
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class Palladium : IPalladium
      {
          public void LogStartingDetails(string truckId, string palladiumId, Stream postData)
          {
              string contents = string.Empty;
              using (var reader = new StreamReader(postData))
              {
                  contents = reader.ReadToEnd();
              }
  
              StreamWriter sw1 =
                  File.AppendText(@&quot;C:\log.txt&quot;);
              sw1.WriteLine(contents);
              sw1.WriteLine(&quot;&quot;);
              sw1.Close();
  
              return;
          }
      }
  }
  </code></pre>
  <p><strong>URITemplate.cs</strong> (in the CDS.PalladiumService project)</p>
  <pre><code>namespace CDS.PalladiumService
  {
      public static class UriTemplate
      {
          public const string LogStartingDetails = &quot;/log-starting/{truckId}/{palladiumId}&quot;;
      }
  }
  </code></pre>