c# – (413)请求实体太大

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

最佳答案
正如Matt Burland建议的那样,您需要配置服务端和客户端.有关详情,请参见Configuring Services Using Configuration Files.任务与您在电线的客户端所做的工作没有太大的不同.这是上述文章的摘录.

WCF uses the System.Configuration configuration system of the .NET
Framework. When configuring a service in Visual Studio, use either a
Web.config file or an App.config file to specify the settings. The
choice of the configuration file name is determined by the hosting
environment you choose for the service. If you are using IIS to host
your service, use a Web.config file. If you are using any other
hosting environment, use an App.config file.

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

The size of the messages that can be received on the wire by services
using the WSHttpBindingBase is bounded by the amount of memory
allocated for each message. This bound on message size is intended to
limit exposure to denial of service (DoS) attacks.

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

转载注明原文:c# – (413)请求实体太大 - 代码日志