简体   繁体   English

C#:监视连接

[英]C#: Monitoring Connections

I am writing an application which has an SslStream / NetworkStream / TcpClient as well as multiple SqlConnection instances. 我正在编写一个具有SslStream / NetworkStream / TcpClient以及多个SqlConnection实例的应用程序。 I am sending and receiving data over them constantly, but as this is an application which accesses multiple servers both locally and remotely, I need to monitor the connections for failure and somehow attempt to restart them should they fail. 我一直在通过它们发送和接收数据,但是由于这是一个可以在本地和远程访问多个服务器的应用程序,因此我需要监视连接是否出现故障,并在出现故障时尝试重新启动它们。 Is there a Microsoft supported convention for how I should monitor these classes? 我应如何监视这些类,是否有Microsoft支持的约定? The documentaton doesn't really show me anything. 该文档并没有真正向我显示任何内容。 I've tried CanRead / CanWrite for the SslStream and I do, of course, have the ability to monitor timeouts. 我已经为SslStream尝试过CanRead / CanWrite ,并且我确实具有监视超时的能力。 Is there a recommended time for timing these out? 是否有建议的时间来使这些超时? Is monitoring/counting timeouts the only way to monitor for connection loss? 监视/计数超时是监视连接丢失的唯一方法吗?

Managed SqlConnection instances should be short lived. 托管SqlConnection实例应该短暂存在。 Only create them when you need them and then dispose them. 仅在需要它们时创建它们,然后对其进行处置。 Do not keep them hanging around in your application for when you want them, that is a very bad practice. 不要让它们在您的应用程序中徘徊,这是一个非常糟糕的做法。 The reason is that Sql Server (as well as other databases) implement connection pooling so creating the managed connection is relatively cheap as the underlying connection will be reused once its released back to the pool. 原因是Sql Server(以及其他数据库)实现了连接池,因此创建托管连接相对便宜,因为一旦将基础连接释放回池中,基础连接就会被重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM