【问题标题】:WCF Service fails with a 1 hour timeout in 30 secondsWCF 服务在 30 秒内因 1 小时超时而失败
【发布时间】:2011-10-20 09:23:45
【问题描述】:

我在本地使用 WCF 服务来计算一些信息,这是 C#,并返回数据。 我返回的数据是浮动列表List<List< float>> 的列表,总共有 4 个。每个浮动列表包含 400 个项目,每个集合中有 180 个这些列表。所以List<List<float>>的4个

我最初遇到空间不足错误,然后将大小更新为最大 2,000,000 字节。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_IExternalBallistics" closeTimeout="01:10:00"
                openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="2000000" maxBufferSize="2000000" maxConnections="10"
                maxReceivedMessageSize="2000000">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="None">
                    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://localhost:6100/ExternalBallistics"
            binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IExternalBallistics"
            contract="IExternalBallistics" name="NetTcpBinding_IExternalBallistics" />
    </client>
</system.serviceModel>
</configuration>

我还在我的主机中手动更新了这些值。

private void InitializeServiceHost()
{
    if (_serviceHost != null)
    {
        _serviceHost.Close();
    }

    Uri address = new Uri("net.tcp://localhost:6100/ExternalBallistics");
    NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
    binding.MaxReceivedMessageSize = 2000000;
    binding.MaxBufferSize = 2000000;
    binding.MaxBufferPoolSize = 2000000;
    binding.CloseTimeout = new TimeSpan(1, 10, 0);
    binding.OpenTimeout = new TimeSpan(1, 10, 0);
    binding.ReceiveTimeout = new TimeSpan(1, 10, 0);
    binding.SendTimeout = new TimeSpan(1, 10, 0);
    _serviceHost = new ServiceHost(typeof(ExternalBallisticsImpl), address);
    _serviceHost.Description.Behaviors.Add(GetMetadataBehavior());
    _serviceHost.CloseTimeout = new TimeSpan(1, 10, 0);
    _serviceHost.OpenTimeout = new TimeSpan(1, 10, 0);
    _serviceHost.AddServiceEndpoint(typeof(IExternalBallistics), binding, address);
    _serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

    _serviceHost.Open();
}

我还将超时设置为 1 小时 10 分钟。

我得到的错误是 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 '01:09:59.9770000'.

现在此超时发生在 30 秒内。我正在运行一个单元测试,并且服务正确执行所有操作,并且回复包含有效数据,但是当它返回此回复时,我总是收到此错误。

我一直在搜索,但我找不到任何可以解决的答案,因为我看到的答案只是告诉我增加我拥有的缓冲区/超时。

【问题讨论】:

  • 您在服务本身中没有遇到任何错误 - 当服务向客户端发送响应时会发生这种情况?

标签: c# wcf service timeout


【解决方案1】:

虽然报告为超时异常,但可能是另一个问题。

您是否可以尝试设置 maxItemsInObjectGraph 以确保您可以发送大型对象图。

  <serviceBehaviors>

    <behavior name="MyBehavior">

      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>

      <serviceMetadata />

    </behavior>

  </serviceBehaviors>

</behaviors>

点击以下链接,希望对您有所帮助:

http://davybrion.com/blog/2008/09/wcf-and-large-amounts-of-data/

【讨论】:

  • 是的,这绝对是答案。当我将它与 unity3D 一起使用时,要为我的解决方案添加更多内容,我不能为该服务使用配置。所以我需要在 WCF 主机代码中定义所有这些。所以我只是从这段代码social.msdn.microsoft.com/Forums/en-US/wcf/thread/… 中提取了foreach 循环,我也必须将它应用到客户端。类似的方法,但是你从channelFactory获取操作描述。
【解决方案2】:

正如@Tim 在他的评论中所建议的,这可能是服务器在大约 30 秒后无法与客户端通信的问题。客户端可能已经关闭了它的连接。

确保客户端上的绑定设置与服务器上的绑定设置相匹配。尤其要确保客户端的receiveTimeout 值与服务器的sendTimeout 相似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多