.net - 采用默认 web.config 并增加 maxStringContentLength 所需的更改

标签 .net wcf .net-4.0 web-config

我需要从我的一个客户向我的服务器发送大量文本。为此,我知道我需要增加服务器上的 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>  

背景资料:

最佳答案

您必须修改服务端点上使用的绑定(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设置 maxStringContentLengthmaxReceivedMessageSize属性设置为相同的值:

The value of the maxStringContentLength attribute cannot be greater than the value of the maxReceivedMessageSize 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/

相关文章:

c# - Awesomium.NET 和 WCF

c# - 如何获取 WCF 客户端故障事件的原因?

c# - 测量 .NET 对象的大小

c# - 如何在C#中使用泛型执行此操作

.net - WPF DataGrid 以编程方式添加复杂列

.net - 多线程自承载 WCF 服务

c# - 等待异步谓词列表,但在第一个 false 时退出

c# - ODBC 命令不提供插入记录的计数

WCF ChannelFactory 没有准确报告故障状态?

c# - 以编程方式在 FlowDocument 中创建水平线