【问题标题】:(413) Request Entity Too Large(413请求实体太大
【发布时间】:2014-12-30 16:17:47
【问题描述】:

我有 WCF 服务,当我想将参数作为大字符串(超过 1mb)传递时,我有一个方法

我运行这个 wcf 并在 WCF 测试客户端中更改了配置,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IMyService" sendTimeout="00:05:00"
                    maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                </binding>
            </basicHttpBinding>
        </bindings>

当我尝试调用此方法时,我仍然有 413 请求实体太大。

【问题讨论】:

  • 您是否在 both 两端都进行了更改?服务器和客户端?
  • 我添加到客户端: 但我仍然遇到同样的错误

标签: c# wcf wcf-binding http-status-code-413


【解决方案1】:

正如 Matt Burland 所建议的,您需要配置服务端和客户端。有关详细信息,请参阅Configuring Services Using Configuration Files。该任务与您在网络客户端所做的工作没有太大区别。这是上述文章的摘录。

WCF 使用 .NET 的 System.Configuration 配置系统 框架。在 Visual Studio 中配置服务时,请使用 Web.config 文件或 App.config 文件来指定设置。这 配置文件名的选择由主机决定 您为服务选择的环境。如果您使用 IIS 托管 您的服务,请使用 Web.config 文件。如果您使用任何其他 托管环境,请使用 App.config 文件。

我建议不要将所有内容都设置为 int.MaxValue,因为将 MaxReceivedMessageSize 设置为 2GB 会使您面临 DOS(拒绝服务)攻击等。 MaxReceivedMessageSize 属性的备注部分甚至声明:

服务可以在线接收的消息的大小 使用 WSHttpBindingBase 受内存量的限制 为每条消息分配。这种对消息大小的限制旨在 限制遭受拒绝服务 (DoS) 攻击。

此时您可能只是想让它工作,但远不建议保持这种方式。

【讨论】:

    【解决方案2】:

    我也遇到过同样的问题并解决了。 工作代码-

    (413) Request Entity Too Large in WCF 遵循 app.config 代码。这是有效的,使用它我可以发送大文件

    <bindings>
        <webHttpBinding>
            <binding name="myBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" transferMode="Streamed" >
                <readerQuotas maxDepth="64" maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
            </binding>
        </webHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="ForecastREST_API.RESTServiceImplBehavior" name="ForecastREST_API.RestServiceImpl">
            <endpoint address="http://localhost:59624/RestServiceImpl.svc" binding="webHttpBinding" contract="ForecastREST_API.IRestServiceImpl" behaviorConfiguration="Web" bindingConfiguration="myBinding">
                </identity>
            </endpoint>
            <endpoint address="mex" binding="webHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <endpointBehaviors>
            <behavior name="Web">
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" />
                <dispatcherSynchronization asynchronousSendEnabled="true" />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="ForecastREST_API.RESTServiceImplBehavior">
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    
    【解决方案3】:

    这对我有用, 在 web.config 应用端:

    <system.serviceModel>
       <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IXXXXXX" 
                maxBufferPoolSize="2147483647" maxBufferSize="2147483647" 
                maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="2147483647" 
                    maxStringContentLength="2147483647" 
                    maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                    maxNameTableCharCount="2147483647"/>
            </binding>
    </system.serviceModel>
    

    在 web.config 服务端,您需要在下一个标签中添加此代码:

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IXXXXXX" 
                maxBufferPoolSize="2147483647" 
                maxBufferSize="2147483647" 
                maxReceivedMessageSize="2147483647" 
                messageEncoding="Text">
              <readerQuotas maxDepth="2000000" 
                  maxStringContentLength="2147483647" 
                  maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                  maxNameTableCharCount="2147483647"/>
            </binding>
          </basicHttpBinding>
        </bindings> 
        <services>
          <service name="ServiciosSoporteVital.WCFCaso">
            <endpoint address=""
                      binding="basicHttpBinding"
                      bindingConfiguration="BasicHttpBinding_IXXXXXX"
                      contract="ServiciosSoporteVital.IWCFCaso"/>
          </service>
        </services>     
    </system.serviceModel>
    

    最重要的是通过两个应用程序中的绑定名称和绑定配置的关系。

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 2012-09-23
      • 2015-10-04
      • 2015-01-25
      • 2014-12-04
      • 2017-08-28
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多