6 Replies Latest reply on Dec 15, 2017 7:21 PM by Mark.Derbecker

    Reliable method of determining connection liveliness

    Mark.Derbecker

      We have an application that queries PIServer (PI) and PISystem (AF) periodically, and we have found that our queries can return PI connection exceptions when the server connection fails for some reason. That's good, and expected.

       

      However, we need to detect this connection failure on a separate thread so that we can disconnect and attempt a reconnect. We have found that the ConnectionInfo.IsConnected property and ConnectChanged event are unreliable for determining connection liveliness. The former will return true even if the network adapter has been disabled, and ConnectChanged doesn't get fired.

       

      Is this a known issue?

       

      As a workaround, for PI, we are calling PIServer.PointCount (instead of ConnectionInfo.IsConnected), which appears to do a round-trip and will throw an exception if the connection is down.

       

      The workaround for AF is to call PISystem.GetRpcMetrics(), which also appears to be a round-trip and will throw an exception.

       

      Are these workarounds a bad idea?

        • Re: Reliable method of determining connection liveliness
          Rick Davin

          Hi Mark,

           

          Those workarounds don't seem to be what you want.  Are you using VB.NET by any chance?  With VB, you will need to use WithEvents to get the PISystem.ConnectChanged to be triggered.  Have you seen this event example before?  It doesn't cover ConnectChanged directly but does show how to define event handlers in VB.NET and C#.

           

          By itself, the ConnectionInfo.IsConnected is not what you would want to check the connection repeatedly on-demand.  It's great for making an initial check to decide whether to connect or not.  But after that, it won't tell you if a connection has been dropped.  It needs something else, and that's where the ConnectChanged event comes in.

           

          I have used PISystem.ConnectChanged and have not had a problem with it firing.  You may need to try a PISystem.Refresh inside your ConnectChange handler to get a refreshed copy of IsConnected.

           

          Can you share the relevant portions of your code that are causing issues?

            • Re: Reliable method of determining connection liveliness
              David Hearn

              As Rick mentioned, the IsConnected method will only indicate if a connection has been made to the server, not the current status of a valid connection. You will need to make a call to the server to determine the current connection status and the GetRpcMetrics is a good one to call since it is fast. It will not query SQL Server, so if the problem is with the connection to SQL Server this RPC will not generate an error.

              • Re: Reliable method of determining connection liveliness
                Mark.Derbecker

                Rick, David,

                 

                Thanks for mention of PISystem.Refresh(). I had always been calling it, and it had no effect on the accuracy of PISystem.ConnectionInfo.IsConnected. However, I decided to try PISystems.Refresh(true). That caused PISystem.ConnectedInfo.IsConnected to be accurate, which is an improvement.

                 

                However, in a "pure PI" case, neither PIServer.Refresh() nor PIServers.Refresh() have any effect on the accuracy of PIServer.ConnectionInfo.IsConnected.

                 

                Here's the code I used to register for the ConnectChanged event. In my experience, this event is completely unreliable in both the PISystem and PIServer cases:

                 

                        override public void Initialize() {
                            this.piServer.ConnectChanged += PiServer_ConnectChanged;
                        }
                
                
                        private void PiServer_ConnectChanged(object sender, EventArgs e) {
                            log.Debug("hey!");
                        }
                

                 

                I think I'll just leave it at that... the PIServer.PointCount call is fast and is an accurate indicator of liveliness in the "pure PI" case.