【问题标题】:Self-hosted NetTcp service timeout error自托管 NetTcp 服务超时错误
【发布时间】:2011-06-17 10:07:27
【问题描述】:

我有一个包含 WCF NetTcp 主机的 Windows 服务。从客户端,当我开始通过 tcp 调用服务时,它们一开始运行良好,但几分钟后它们都开始出现可怕的 WCF 超时错误,这与超时无关:

发送到 net.tcp://myserver:8080/ListingService 的请求操作未在配置的超时 (00:01:00) 内收到回复。

我从该网站上的其他帖子中看到,很多时候这与最大邮件大小有关,但我已经将这些设置为无用的限制。

这是我的 Windows 服务代码:

public partial class Service : ServiceBase
    {
        internal static ServiceHost myHost = null;

        public Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 10000;
            //create host.
            var path = ConfigurationManager.AppSettings["ServiceHostAddress"].ToString();
            myHost = new ServiceHost(typeof(ListingService));
            //add endpoint.
            myHost.AddServiceEndpoint(typeof(IListingService), GetBinding(), path);
            //add behaviors.
            AddBehaviors();
            //open host.
            myHost.Open();
        }

        private void AddBehaviors()
        {
            //service metadata behavior.
            var smb = new ServiceMetadataBehavior();
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            myHost.Description.Behaviors.Add(smb);
            //service throttling behavior.
            var behavior = new ServiceThrottlingBehavior()
            {
                MaxConcurrentCalls = 10000,
                MaxConcurrentInstances = 10000,
                MaxConcurrentSessions = 10000
            };
            myHost.Description.Behaviors.Add(behavior);
            //service debug behavior.
            var serviceDebugBehavior = myHost.Description.Behaviors.Find<ServiceDebugBehavior>();
            serviceDebugBehavior.IncludeExceptionDetailInFaults = true;
        }

        private Binding GetBinding()
        {
            var queueBinding = new NetTcpBinding(SecurityMode.None);
            queueBinding.MaxConnections = 10000;
            queueBinding.MaxBufferSize = 2048000;
            queueBinding.MaxReceivedMessageSize = 2048000;
            return queueBinding;
        }

        protected override void OnStop()
        {
            if (myHost != null)
            {
                myHost.Close();
                myHost = null; 
            }
        }
    }

这是客户端配置,以防万一:

<system.serviceModel>
    <bindings>

      <netTcpBinding>
        <binding name="NetTcpBinding" transferMode="Buffered" hostNameComparisonMode="StrongWildcard"
          closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
          maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"><!-- transactionFlow="true"-->
          <security mode="None"/>
          <reliableSession enabled="false"/>
          <readerQuotas maxArrayLength="2147483647"/>
        </binding>
      </netTcpBinding>

    </bindings>
    <client>

      <endpoint
             address="net.tcp://myserver:8080/ListingService"
             binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
             contract="ListingServiceProxy.IListingService" name="NetTcpBinding" />

    </client>
  </system.serviceModel>

我确保关闭我的客户端连接,代码如下:

public static void Using<T>(this T client, Action<T> work)
            where T : ICommunicationObject
        {
            try
            {
                work(client);
                client.Close();
            }
            catch (CommunicationException)
            {
                client.Abort();
                throw;
            }
            catch (TimeoutException)
            {
                client.Abort();
                throw;
            }
            catch
            {
                client.Abort();
                throw;
            }
        }

new ListingServiceClient().Using(client =>
                {
                    client.SaveListing(listing);
                });

【问题讨论】:

  • 您是否优雅地关闭了客户端代理?这看起来像是服务限制行为。服务节流控制 ServiceHost 可以处理多少并发调用、会话和实例。 NetTcp 是面向会话的绑定 - 每个新的客户端代理都会创建新的会话和服务实例,以处理来自同一代理实例的所有请求。
  • 嗨@Ladislav Mrnka 我在上面添加了客户端代码,它应该在每次请求后关闭客户端。我有一个问题......如果我启用了可靠会话,会有什么不同吗?

标签: wcf timeout nettcpbinding servicehost


【解决方案1】:

他们最终超时了,因为数据库实际上超时了,这不是 WCF 的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2012-09-05
    • 2012-01-18
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多