WCF 可能超出了我的响应大小

标签 wcf binding maxreceivedmessagesize

好的,我的 WCF 服务终于运行良好了。这花了我一些时间,但我已经解决了几乎所有的问题。我现在遇到的唯一问题是这个。

我的一个数据请求相当大,它包含一个大约 37k 的集合。我得到的错误如下。

The socket connection was aborted. This could be caused by an error processing 
your message or a receive timeout being exceeded by the remote host, or an 
underlying network resource issue. Local socket timeout was '00:00:59.9840000'.

最初您可能会认为这是一个序列化问题,或者我需要更改绑定(bind)的容量。

这是我在客户端的绑定(bind)。

<netTcpBinding>
<binding name="MyCustomBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                receiveTimeout="00:10:00" sendTimeout="00:01:00" 
                transactionFlow="false" transferMode="Buffered" 
                transactionProtocol="OleTransactions" 
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="1000000000" maxBufferSize="1000000000" 
                maxConnections="10" maxReceivedMessageSize="1000000000">
                <readerQuotas maxDepth="32" maxStringContentLength="1000000000" 
                    maxArrayLength="50000" maxBytesPerRead="4096" 
                    maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Transport">
                    <transport clientCredentialType="Windows" 
                               protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
</netTcpBinding>

我已经更改了我的绑定(bind)信息以允许更大的容量;但是,这并不能解决我的问题。我开始考虑更改 maxReceiveMessageSize 容量的唯一原因是,如果我将结果限制为 1000 个集合,我就没有问题。当涉及到大型通信时,除了绑定(bind)之外,还有什么配置是我需要担心的吗?

谢谢!

编辑 1

此外,我已将绑定(bind)中的每个超时值的超时更改为 00:10:00,这似乎没有什么不同。服务器的响应不到 5 秒。

编辑2

服务配置。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
</configSections>
<system.web>
   <compilation debug="true" />
</system.web>
<system.serviceModel>
<bindings />
<client />
<services>
  <service behaviorConfiguration="serviceBehavior"                      
           name="MyService">
    <clear />
    <endpoint address="MyCustomObject" binding="netTcpBinding" 
              bindingConfiguration="" name="MyCustomObject" 
              contract="MyService.IContract" >
    </endpoint>

    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8732/MyService/" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>

编辑 3

请理解,我没有包含有关该服务模型的任何敏感信息,因此它们已被替换为 MyCustom***。再次,请仔细阅读问题我使用此服务通过其他端点成功地来回传输数据没有问题。另外,正如我上面所说,如果我将结果限制为 1000,我可以跨该端点传输数据。

编辑 4

好吧,让我更清楚一点。客户端应用程序正在获取正确的绑定(bind)。我知道的原因是,当我的应用程序在一个集合中返回大约 4500 个结果的结果集时,会出现以下异常。

The maximum message size quota for incoming messages (65536) has 
been exceeded. To increase the quota, use the MaxReceivedMessageSize 
property on the appropriate binding element.

所以我将 MaxReceiveMessageSize 增加到可能的最高值 2147483647。根据这个 post .在我这样做之后,我可以安全地返回我的结果集。现在更重要的是,我得到了 5000 个结果,我在这个问题的顶部得到了我的初始异常。

The socket connection was aborted. This could be caused by an error processing 
your message or a receive timeout being exceeded by the remote host, or an 
underlying network resource issue. Local socket timeout was '00:00:59.9840000'.

很明显,应用程序正在选择正确的绑定(bind)配置,否则它不会响应我在 MaxReceiveMessageSize 中的更改。所以在这一点上真正的问题是为什么它会失败这么小的变化。

最佳答案

最后!好的,这就是问题所在。我超过了客户端的 MaxItemsInObjectGraph 值。所以我需要将此行为添加到客户端配置文件中。

服务器配置

<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior" >
      <serviceMetadata />
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

客户端配置

<behaviors>
<endpointBehaviors>
  <behavior name="endpointBehavior">
    <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
  </behavior>
</endpointBehaviors>
</behaviors>

然后在客户端配置文件端点上,我必须添加该行为。

客户端配置

<client>
<endpoint address="net.tcp://localhost:8732/RootAddress/EndpointLocation"
          binding="netTcpBinding" bindingConfiguration="EndpointLocation"
          behaviorConfiguration="endpointBehavior"
          contract="ServiceReference1.IInterface" name="Interface">
   <identity>
     <userPrincipalName value="" />
   </identity>
</endpoint>
</client>

请注意,我必须将行为部分完全添加到客户端的配置中。因为它不是在我使用添加服务引用时生成的。这之后工作完美,我能够检索我的 37k 自定义对象集合。

关于WCF 可能超出了我的响应大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418938/

相关文章:

wpf - ListBox.ItemTemplate 中的数据绑定(bind)用户控件

silverlight - 使用 SSL 的 WCF 服务数据限制

asp.net - 如何使用多个绑定(bind)将 WCF json 绑定(bind)到 https?

.net - 如何在没有 try-catch 的情况下检测 WCF 连接?

c# - 在 C# 中以编程方式创建 WCF 客户端的 header (wsse) 部分

binding - 我可以在 JavaFX 中创建弱绑定(bind)吗?

WPF ComboBox SelectedItem 在 TabControl 开关上设置为 Null

c# - 如何通过 HTTP 与 MSMQ 通信?

wcf - WCF 4 REST 服务的正确配置是什么?

WCF MaxReceivedMessageSize 属性未采用