Unable to set maxReceivedMessageSize through web.config

孤街浪徒 提交于 2019-11-28 08:43:13
gimlichael

All right, this one really caused me a hard time resolving, which I will spare others for. The challenge was in the fact, that I used the <%@ ServiceHost Factory="System.ServiceModel.Activation.WebServiceHostFactory" Service="fullyQualifiedClassName" %>, which is a nice and easy factory implementation approach.

However, this approach has it drawbacks; since no configuration is needed in the web.config file, the WebServiceHostFactory class by design does not ever read from the web.config file. I know; I could inherit from this class, and make the appropriate changes so it may indeed read from the config file, but this seemed a little out of scope.

My solution was to go back to the more traditional way of implementing the WCF; <%@ ServiceHost Service="fullyQualifiedClassName" CodeBehind="~/App_Code/Catalogue.cs" %>, and then use my already configured values in the web.config file.

Here is my modified web.config file (with respect to Maddox headache):

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="XmlMessageBinding" maxReceivedMessageSize="5000000" maxBufferPoolSize="5000000" maxBufferSize="5000000" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
          <readerQuotas maxStringContentLength="5000000" maxArrayLength="5000000" maxBytesPerRead="5000000" />
          <security mode="None"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="fullyQualifiedClassName" behaviorConfiguration="DevelopmentBehavior">
        <endpoint name="REST" address="" binding="webHttpBinding" contract="fullyQualifiedInterfaceName" behaviorConfiguration="RestEndpointBehavior" bindingConfiguration="XmlMessageBinding" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="RestEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="DevelopmentBehavior">
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
        <behavior name="ProductionBehavior">
          <serviceDebug httpHelpPageEnabled="false" includeExceptionDetailInFaults="false"/>
          <serviceMetadata httpGetEnabled="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Another benefit of this change is, that you can now reference your WCF-rest service directly from .NET; this cannot be done using the Factory model and my implementation of XmlElement through out the solution.

I hope this can help others with similar issues ...

I know this is a very old Question and it already has an answer...

Anyway...

What I did to solve this "issue" I created a Factory inherited from WebServiceHostFactory and created a Custom Service Host inherited from WebServiceHost

And in the host I overrode the OnOpening method like this

protected override void OnOpening()
        {
            base.OnOpening();

            foreach (var endpoint in Description.Endpoints)
            {
                var binding = endpoint.Binding as System.ServiceModel.Channels.CustomBinding;

                foreach (var element in binding.Elements)
                {
                    var httpElement = element as System.ServiceModel.Channels.HttpTransportBindingElement;
                    if (httpElement != null)
                    {
                        httpElement.MaxBufferSize = 2147483647;
                        httpElement.MaxReceivedMessageSize = 2147483647;
                    }
                }
            }

        }

I think i had the same issue, but when i configured the default-binding for webHttp then it worked:

<bindings>
        <webHttpBinding>
            <binding maxReceivedMessageSize="2000000"
                      maxBufferSize="2000000">
                <readerQuotas maxStringContentLength="2000000"/>
            </binding>
        </webHttpBinding>
    </bindings>

Observe: no name on the binding.

This is a blog entry I wrote that reproduces this problem with an absolutely minimal WCF server and client piece:

WCF - Fixing client side string length exceptions

In particular, you may need a Custom Binding Configuration. At least reproducing this sample may give you some ideas for your particular situation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!