c# - WCF 的字符串太长

标签 c# .net wcf string

我正在尝试通过 WCF 发送一个长字符串,大约 64k 个字符长。发送长字符串时,我收到 HTTP 错误 400。但是当我发送较短的字符串时,一切正常。 这是我使用的 WCF 接口(interface)和 app.config。

我的消息合约:

[MessageContract]
public class MessageClass
{
    [MessageHeader(MustUnderstand = true)]
    public string id;

    [MessageBodyMember(Order=1)]
    public string realMessage;   // Long string
}

我试图通过提高值来更改 app.config 设置:

<bindings>
  <basicHttpBinding>
    <binding
      name="ws"
      transferMode="Streamed"
      messageEncoding="Mtom"
      maxReceivedMessageSize="10067108864">
      <readerQuotas
        maxDepth="32"
        maxStringContentLength="2147483647"
        maxArrayLength="2147483647"
        maxBytesPerRead="4096"
        maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

还有其他我应该更改的值吗?

最佳答案

您还需要在绑定(bind)上设置“maxBufferSize”和“maxBufferPoolSize”:

<bindings>
  <basicHttpBinding>
    <binding
      name="ws"
      transferMode="Streamed"
      messageEncoding="Mtom"
      maxReceivedMessageSize="10067108864"
      maxBufferSize="500000" maxBufferPoolSize="500000">
      <readerQuotas
        maxDepth="32"
        maxStringContentLength="2147483647"
        maxArrayLength="2147483647"
        maxBytesPerRead="4096"
        maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

在 WCF 的标准绑定(bind)中,它们也默认为 64K。但是,由于您使用的是“transferMode=Streamed”,这真的不应该成为问题 - 也许还有其他事情正在发生。增加 sendTimeout 设置怎么样?也许您的服务响应时间有点长。

马克

关于c# - WCF 的字符串太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1403558/

相关文章:

c# - 使用 DataReader 添加列值

c# - 将 OperationContext 传播到异步 WCF 调用中

c# - 如何从 Azure webjob 到 Azure webapp 进行通信?

c# - 使用 Silverlight 适当实现 IDisposable?

c# - 是否可以使用 WCF 实现长时间运行的多次写入事务?

c# - WCF - 自定义绑定(bind)的配置

c# - LINQ扩展方法求助二

c# - 添加多个文本框.net

c# - 单独的平滑进出缩放

.net - 所有不同的X509KeyStorageFlags的基本原理是什么?