我需要从我的一个客户向我的服务器发送大量文本。为此,我知道我需要增加服务器上的 maxStringContentLength
。
我对此进行了大量搜索,似乎所有针对此问题的修复都从第 3 步开始。
我似乎无法弄清楚第 1 步和第 2 步...
谁能陪我度过这个美好而缓慢的过程。鉴于下面的 Web.config,我如何才能设置 maxStringContentLength
?
这是我的 Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
背景资料:
- 运行 Visual Studio 2010
- .NET 4
- 在 IIS 7 中托管
- 名为 Host.IIS 的 VS 项目
- WSDL URL = http://MyServer/Orders/OrdersService.svc?wsdl
最佳答案
您必须修改服务端点上使用的绑定(bind)属性。由于您没有在配置文件中提供任何内容,WCF 4.0 是 automatically going to add a default endpoint for each service contract and base address defined in the host.如果服务托管在 IIS 中,则基址是虚拟目录。
默认端点上使用的绑定(bind)在 <protocolMapping>
中定义元素。在机器级别,映射是:
<protocolMapping>
<add scheme="http" binding="basicHttpBinding"/>
<add scheme="net.tcp" binding="netTcpBinding"/>
<add scheme="net.pipe" binding="netNamedPipeBinding"/>
<add scheme="net.msmq" binding="netMsmqBinding"/>
</protocolMapping>
在您的情况下,除非已覆盖默认映射,否则 WCF 会根据 http://myserver/Orders
创建一个默认的 HTTP 端点。使用 basicHttpBinding 的虚拟目录.
您可以修改 maxStringContentLength
basicHttpBinding 的属性通过提供默认绑定(bind)配置,这是一个无名配置,它将自动应用于使用该绑定(bind)的所有端点。
只需将此元素添加到您的 Web.config 中的 <system.serviceModel>
部分:
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="SomeIntegerValue">
<readerQuotas maxStringContentLength="SomeIntegerValue" />
</binding>
</basicHttpBinding>
</bindings>
请注意 MSDN recommends设置 maxStringContentLength和 maxReceivedMessageSize属性设置为相同的值:
The value of the
maxStringContentLength
attribute cannot be greater than the value of themaxReceivedMessageSize
attribute. We recommend that the values of the two attributes are the same.
如果这不起作用,您可以通过添加 <protocolMapping>
显式定义用于默认 HTTP 端点的绑定(bind)。 Web.config 中的元素并相应地调整绑定(bind)配置:
<protocolMapping>
<add scheme="http" binding="basicHttpBinding"/>
</protocolMapping>
关于.net - 采用默认 web.config 并增加 maxStringContentLength 所需的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9334252/