c# - (413请求实体太大

标签 c# wcf wcf-binding http-status-code-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.

我建议不要像 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/

相关文章:

c# - MVC4 c# 获取office word文档,并转换为列表

wcf - WCF REST Starter Kit 是否不再是创建 Restful 服务的最佳方式?

wcf - 离线分享WCF接口(interface)规范

.net - 以编程方式配置 wcf 服务

c# - BooleanToVisibilityConverter 带复选框

c# - 使用 Azure Function (.NET Core) 下载文件

c# - 是 type.isSubclassOf(Type otherType) 缓存还是我必须自己做?

wcf - .NET 4.5 中是否支持带有异步方法的 ChannelFactory?

wcf - 为每个工作流程定义不同的默认 ServiceHostFactory

.net - WSHttpBinding 和 Https 问题