WCF服务在30秒内出现1小时超时失败

本文关键字:1小时 超时 失败 服务 30秒 WCF | 更新日期: 2023-09-27 18:04:38

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

我最初有一个空间不足的错误,然后将大小更新为最大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秒内。我有一个正在运行的单元测试,服务做的一切都正确,回复包含有效数据,但是当它返回这个回复时,我总是得到这个错误。

我一直在搜索,我无法得到任何答案,因为我所看到的只是通知我增加缓冲区/超时,我有。

WCF服务在30秒内出现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/

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

确保客户机上的绑定设置与服务器上的绑定设置相匹配。特别是,要确保客户机的receiveTimeout值与服务器的sendTimeout值相似。

所以,我必须转到远程服务器的IIS管理器。点击左侧菜单上的"应用程序池",选择"我的网站",然后在"高级设置"(在右侧)中,我将"启动模式"设置为"始终运行",而不是"按需运行"。