无法通过web.config设置maxReceivedMessageSize

gim*_*ael 8 rest web-config request webhttpbinding

我现在已经调查了过去两小时的400-BadRequest代码.很多sugestions用于确保正确设置bindingConfiguration属性,在我的例子中,它是.

现在,在摧毁我所在的建筑物之前,我需要你的帮助:-)

我运行WCF RestFull服务(非常轻量级,使用此资源获取灵感:http://msdn.microsoft.com/en-us/magazine/dd315413.aspx),它(现在)接受通过提供的XmlElement(POX) POST动词.

在实现真正的客户端之前,我目前只使用Fiddler的请求构建器(因为这是混合环境).

当我为小于65K的XML执行此操作时,它工作正常 - 更大,它会抛出此异常:已超出传入消息的最大邮件大小配额(65536).要增加配额,请在相应的绑定元素上使用MaxReceivedMessageSize属性.

这是我的web.config文件(我甚至包括了客户端标签(绝望的时候!)):

<system.web>
    <httpRuntime maxRequestLength="1500000" executionTimeout="180"/>
  </system.web>
  <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
    </diagnostics>
    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBinding" maxReceivedMessageSize="1500000" maxBufferPoolSize="1500000" maxBufferSize="1500000" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
          <readerQuotas maxStringContentLength="1500000" maxArrayLength="1500000" maxBytesPerRead="1500000" />
          <security mode="None"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <client>
      <endpoint address="" binding="webHttpBinding" bindingConfiguration="WebHttpBinding" contract="Commerce.ICatalogue"/>
    </client>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Catalogue">
        <endpoint address="" 
                  behaviorConfiguration="RestFull" 
                  binding="webHttpBinding"
                  bindingConfiguration="WebHttpBinding" 
                  contract="Commerce.ICatalogue" />
        <!-- endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" / -->
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="RestFull">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

提前感谢任何帮助导致成功调用> 65K XML ;-)

gim*_*ael 13

好吧,这个真的让我很难解决,我会为其他人做好准备.事实上,我使用的<%@ ServiceHost Factory="System.ServiceModel.Activation.WebServiceHostFactory" Service="fullyQualifiedClassName" %>是挑战,这是一种简单易用的工厂实施方法.

然而,这种方法有它的缺点; 由于web.config文件中不需要任何配置,因此WebServiceHostFactory类不会从web.config文件中读取.我知道; 我可以继承这个类,并进行适当的更改,因此它可能确实从配置文件中读取,但这似乎有点超出范围.

我的解决方案是回到更传统的实施WCF的方式; <%@ ServiceHost Service="fullyQualifiedClassName" CodeBehind="~/App_Code/Catalogue.cs" %>,然后在web.config文件中使用我已配置的值.

这是我修改过的web.config文件(关于Maddox头痛):

<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>
Run Code Online (Sandbox Code Playgroud)

此更改的另一个好处是,您现在可以直接从.NET引用您的WCF-rest服务; 在整个解决方案中,无法使用Factory模型和我的XmlElement实现来完成此操作.

我希望这可以帮助其他类似问题...


jjc*_*hiw 6

我知道这是一个非常古老的问题,它已经有了答案......

无论如何...

我做了什么来解决这个"问题"我创建了一个继承自WebServiceHostFactory的工厂并创建了一个继承自WebServiceHost的自定义服务主机

在主持人中,我像这样覆盖了OnOpening方法

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;
                    }
                }
            }

        }
Run Code Online (Sandbox Code Playgroud)


Gof*_*off 6

我想我有同样的问题,但当我为webHttp配置默认绑定时,它工作:

<bindings>
        <webHttpBinding>
            <binding maxReceivedMessageSize="2000000"
                      maxBufferSize="2000000">
                <readerQuotas maxStringContentLength="2000000"/>
            </binding>
        </webHttpBinding>
    </bindings>
Run Code Online (Sandbox Code Playgroud)

观察:绑定上没有名称.