Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF WebServiceHostFactory MaxReceivedMessageSize configuration
    primarykey
    data
    text
    <p>I have a <strong>RESTful WCF web service</strong> called "Palladium" 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 "Palladium.svc". </p> <p>My service works in a manner similar to the one explained <a href="http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx" rel="nofollow">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="C#" Debug="true" Service="CDS.PalladiumService.Palladium" Factory="CDS.PalladiumService.MyWebServiceHostFactory" %&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 "IPalladium" here, you must also update the reference to "IPalladium" in Web.config. [ServiceContract] public interface IPalladium { [OperationContract] [WebInvoke(Method = "POST", 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 "Palladium" here, you must also update the reference to "Palladium" 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(@"C:\log.txt"); sw1.WriteLine(contents); sw1.WriteLine(""); 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 = "/log-starting/{truckId}/{palladiumId}"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload