有关检查WCF连接的问题已打开

Leo*_* Vo 2 .net c# wcf winforms

我有关于检查WCF连接是否打开的问题。我的WCF连接是双向的。我使用State属性来检查客户端的连接状态。我的功能:

private bool ConnectionIsOpen()
{
    if (m_Service != null && (m_Service.State | CommunicationState.Opened) == CommunicationState.Opened)
    {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个服务,该服务每10秒运行一次线程,以检查连接状态。我使用ConnectionIsOpen()方法进行检查。在Windows XP上运行时一切都很好。但是,在Windows 7上运行时会出现问题。

当我拔下网络电缆以创建要断开的连接时,如果在Windows XP上运行应用程序,则检查连接状态为“故障”,但如果在Windows 7上运行,则检查连接的状态仍为“打开”。

在这种情况下,任何人都可以帮助我如何检查连接是否打开。谢谢。

Kie*_*one 5

永远是true

(m_Service.State | CommunicationState.Opened) == CommunicationState.Opened

例如m_Service.State= 0:

0 | CommuncationState.Opened == CommuncationState.Opened

您想改用&(AND)。