Unable to set maxReceivedMessageSize through web.config

前端 未结 4 437
北荒
北荒 2020-12-09 12:58

I have now investigated the 400 - BadRequest code for the last two hours. A lot of sugestions goes towards ensuring the bindingConfiguration attribute is set correctly, and

相关标签:
4条回答
  • 2020-12-09 13:19

    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.

    0 讨论(0)
  • 2020-12-09 13:20

    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.

    0 讨论(0)
  • 2020-12-09 13:21

    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 ...

    0 讨论(0)
  • 2020-12-09 13:33

    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;
                        }
                    }
                }
    
            }
    
    0 讨论(0)
提交回复
热议问题