我有 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.
我建议不要像 MaxReceivedMessageSize
那样将所有内容都设置为 int.MaxValue
设置为 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请求实体太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26723894/