7

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

4

3 回答 3

5

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

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

我建议不要将所有内容都设置int.MaxValueMaxReceivedMessageSize设置为 2GB,这会使您面临 DOS(拒绝服务)攻击等。该MaxReceivedMessageSize物业的备注部分甚至指出:

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

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

于 2014-11-04T00:05:49.073 回答
4

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

(413) 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>
于 2016-07-12T11:49:21.830 回答
0

这对我有用,在 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 的关系。

于 2019-02-26T15:24:14.220 回答