Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem about checking a WCF connection is opened

I have a problem about checking a WCF connection is opened. My WCF Connection is bi-directional. I use State property to check the connection's state at client. My function:

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

I create a service which is a thread running every 10 seconds to check the connection's state. I use the method ConnectionIsOpen() for checking. Everything is well on running on Windows XP. However there is a problem when running on Windows 7.

When I unplug the network cable to create to disconnect, If running application on Windows XP, checking connection's State is Faulted but if running on Windows 7, checking connection' State is still Opened.

Anyone can help me how to check a connection is openned or not in this case. Thanks.

like image 394
Leo Vo Avatar asked Mar 18 '26 21:03

Leo Vo


2 Answers

This will always be true:

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

Example, m_Service.State = 0:

0 | CommuncationState.Opened == CommuncationState.Opened

You want to use & (AND) instead.

like image 183
Kieren Johnstone Avatar answered Mar 20 '26 12:03

Kieren Johnstone


We ran into a similar problem in our own system; disconnecting the network cable or placing either the client machine or the server in sleep mode does not generate a channel fault.

From what I can tell, it seems that the connection state only indicates the state of the connection after the last call and not the current connection state. The only way to know the current state is to actually call the service.

If your client doesn’t need to call the service that often but must react if the connection is lost one solution is to implement a dummy call on the client side which periodically polls the service. If the connection is unavailable when the dummy call is made you’ll get a channel fault that you can then deal with.

The catch is you can’t simply use the dummy call to guarantee that the next call to the service will work:

public void SomeMethode()
{
    if (ConnectionIsOpen())
    {
        m_Service.Dummy();
        // Connection is lost here
        m_Service.SomeMethode();
    }
}

To get around this problem, we implemented a system that automatically re-executes any failed service calls which generate a channel fault after the connection has been restored.

like image 23
Eric v Avatar answered Mar 20 '26 13:03

Eric v



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!