AnsweredAssumed Answered

The requested server is not currently available

Question asked by johnkesselman on Oct 15, 2019
Latest reply on Oct 30, 2019 by gregor

My PI access takes place from two different components of our system.

 

1. Application pool of a web app (IIS)

2. Windows service

 

Both are running under the same user and the IIS has no problems, but the service receives "The requested server is not currently available" error when attempting to get the reference to the PISDK.Server instance. Attached is the code that triggers the error. I'm trying to use windows authentication in this case. Any suggestions would be appreciated.

 

 

public PiServer GetPiServer(bool throwException = true)
{
   PiServer server = null;

   if (PiSupport.IsPISDKInstalled())
   {
      using (ProcedureManagerDBEntities model = new ProcedureManagerDBEntities())
      {
         pi_server_config config = model.pi_server_config.FirstOrDefault();

         if (config != null)
         {
            if (!string.IsNullOrWhiteSpace(config.host_name))
            {
               try
               {
                  _log.Debug($"PI Server host name \"{config.host_name}\"");
                  Dns.GetHostEntry(config.host_name);
                  lock (s_piLock)
                  {
                     if (s_pi_sdk_object == null)
                        s_pi_sdk_object = new PISDK.PISDK();

                     _log.Debug($"Getting PIServer from PI SDK object");
                     var piServer = s_pi_sdk_object.Servers[config.host_name];

                     s_piServerMap.Where(p => !p.Value.TryGetTarget(out PiServer weakServerValue))
                     .ToList().ForEach(p =>
                           {
                              s_piServerMap.Remove(p.Key);
                           });

                     _log.Debug($"Trying to reuse a previously created PIServer");
                     if (s_piServerMap.ContainsKey(piServer))
                        s_piServerMap[piServer].TryGetTarget(out server);

                     

                     if (server == null)
                     {
                        _log.Debug($"Can't reuse so creating a new PIServer");
                        s_piServerMap[piServer] = new WeakReference<PiServer>(server = new PiServer(piServer));
                     }

                     if (server.IsOnline)
                     {
                        _log.Debug($"Opening a PI connection");
                        if (config.use_windows_auth)
                           server.Open("Trusted_Connection=Yes");
                        else
                           server.Open($"UID={config.user_name};PWD={Crypto.Decrypt(config.password)}");

                        _log.Debug($"PI connection is {(server.Connected ? "open" : "NOT open")}");
                     }
                  }
               }
               catch (Exception ex)
               {
                  if (throwException)
                     throw new InvalidPIConfiguration("The PI server configuration is invalid.", ex);
               }
            }
            else if (throwException)
               throw new Exception("The PI server configuration is not correctly configured.");
          }
          else if (throwException)
               throw new Exception("The PI server configuration has not been saved to the database.");
         }
      }
      else if (throwException)
            throw new Exception("The PI SDK is not installed.");

      return server;
}

Outcomes