无法通过 web.config 设置 maxReceivedMessageSize

发布于 2024-08-05 05:40:01 字数 2678 浏览 9 评论 0原文

我现在已经调查了过去两个小时的 400 - BadRequest 代码。 许多建议都是为了确保正确设置 BindingConfiguration 属性,就我而言,确实如此。

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

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

在实现真正的客户端之前,我目前仅使用 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>

提前感谢您使用 > 65K XML 成功调用的任何帮助;-)

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 in my case, it is.

Now, I need YOUR help before destroying the building i am in :-)

I run a WCF RestFull service (very lightweight, using this resource for inspiration: http://msdn.microsoft.com/en-us/magazine/dd315413.aspx) which (for now) accepts an XmlElement (POX) provided through the POST verb.

I am currently ONLY using Fiddler's request builder before implementing a true client (as this is mixed environments).

When I do this for XML smaller than 65K, it works fine - larger, it throws this exception:
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.

Here is my web.config file (which I even included the client-tag for (desperate times!)):

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

Thanks in advance for any help leading to succesfull call with >65K XML ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

马蹄踏│碎落叶 2024-08-12 05:40:01

好吧,这个问题确实让我很难解决,我就饶过其他人吧。
挑战在于,我使用了 <%@ ServiceHost Factory="System.ServiceModel.Activation.WebServiceHostFactory" Service="filledQualifiedClassName" %>,这是一个很好且简单的工厂实施办法。

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

我的解决方案是回到更传统的 WCF 实现方式; <%@ ServiceHost Service="filledQualifiedClassName" 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>

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

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

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

冷了相思 2024-08-12 05:40:01

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

无论如何...

我为解决这个“问题”所做的事情我创建了一个从 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;
                    }
                }
            }

        }

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

        }
走走停停 2024-08-12 05:40:01

我想我也有同样的问题,但是当我配置 webHttp 的默认绑定时,它起作用了:

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

观察:绑定上没有名称。

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.

等往事风中吹 2024-08-12 05:40:01

这是我写的一篇博客文章,它用绝对最小的 WCF 服务器和客户端片段重现了这个问题:

WCF - 修复客户端字符串长度异常

特别是,您可能需要自定义绑定配置。至少复制此示例可以为您提供针对您的特定情况的一些想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文