(413请求实体太大

问题描述 投票:7回答:3

我有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个请求实体太大了。

c# wcf wcf-binding http-status-code-413
3个回答
5
投票

正如Matt Burland建议的那样,您需要配置服务端以及客户端。有关详细信息,请参阅Configuring Services Using Configuration Files。这项任务与您在电线的客户端所做的工作没有太大的不同。这是上述文章的摘录。

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

我建议不要将所有内容设置为int.MaxValue,因为将MaxReceivedMessageSize设置为2GB会打开DOS(拒绝服务)攻击等。 MaxReceivedMessageSize财产的备注部分甚至指出:

使用WSHttpBindingBase的服务可以在线路上接收的消息的大小受到为每条消息分配的内存量的限制。这种对邮件大小的约束旨在限制拒绝服务(DoS)攻击的风险。

你可能只是想让它在这一点上起作用,但是建议不要这样做。


4
投票

我也面临同样的问题并解决了这个问题。工作代码 -

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

-1
投票

这适用于我,在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>

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

© www.soinside.com 2019 - 2024. All rights reserved.