Here are some pointers when using the Connection object. This object is part of the BaseAPI from which each API inherit (SmartObjectClientServer, SmartObjectManagementServer etc).

 

Below is some sample code based on three scenarios in which you can connect to the server from the API.

 

1.       Scenario 1: Opening and closing multiple connections

a.       This is the most expensive way of using connections and causes the most overhead. Reason being that after each Open method on the connection object, the call is authenticated on the server and this is what cause the most overhead. This scenario is typically used in a stateless environment where user context does not exit between method calls for example web apps with no sessions.

 

            SmartObjectManagementServer _mgmtServer = new SmartObjectManagementServer();

            SmartObjectClientServer _clientServer = new SmartObjectClientServer();

 

            string _conString = "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=blackpearl;Port=5555";

 

            SCConnectionStringBuilder _connBuilder = new SCConnectionStringBuilder(_conString);

 

            #region Scenario 1 - Connection for Each operation

 

            //Get SmartObject List...

            _mgmtServer.CreateConnection(_connBuilder.ToString());

            _mgmtServer.Connection.Open(_connBuilder.ToString());

            SmartObjectExplorer smoExplorer = _mgmtServer.GetSmartObjects(SmartObjectInfoType.System);

            _mgmtServer.Connection.Close();

 

 

            if (_clientServer.Connection == null)

                _clientServer.CreateConnection();

            _clientServer.Connection.Open(_connBuilder.ToString());

            SmartObject smo = _clientServer.GetSmartObject(smoExplorer.SmartObjects["UMUser"].Guid);

            _clientServer.Connection.Close();

 

            if (_clientServer.Connection == null)

                _clientServer.CreateConnection();

 

            smo.MethodToExecute = "Get_Users";

            smo.ListMethods[smo.MethodToExecute].Parameters["Label_Name"].Value = "k2";

 

            _clientServer.Connection.Open(_connBuilder.ToString());

            SmartObjectList userList = _clientServer.ExecuteList(smo);

            _clientServer.Connection.Close();

 

            #endregion

 

2.       Scenario 2 : One connection performing many operations.

a.       This is a much more efficient way of using connections. You can share the connection object between API’s thereby making it easier to manage the connection.

 

            SmartObjectManagementServer _mgmtServer = new SmartObjectManagementServer();

            SmartObjectClientServer _clientServer = new SmartObjectClientServer();

 

            string _conString = "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=blackpearl;Port=5555";

 

            SCConnectionStringBuilder _connBuilder = new SCConnectionStringBuilder(_conString);

 

            _mgmtServer.CreateConnection();

            _clientServer.Connection = _mgmtServer.Connection;

 

            _mgmtServer.Connection.Open(_connBuilder.ToString());

            SmartObjectExplorer smoExplorer = _mgmtServer.GetSmartObjects(SmartObjectInfoType.System);

            SmartObject smo = _clientServer.GetSmartObject(smoExplorer.SmartObjects["UMUser"].Guid);

            smo.MethodToExecute = "Get_Users";

            smo.ListMethods[smo.MethodToExecute].Parameters["Label_Name"].Value = "k2";

            SmartObjectList userList = _clientServer.ExecuteList(smo);

 

            _mgmtServer.Connection.Close();

 

3.       Scenario 3: Using sessions.

a.       If your calling client has state and can manage sessions, this is the way to use it with the connection object. The session connection timeout on the server can be set. It does however apply  to the entire server which means all servers hosted on that server. The default time is 20 minutes. If a session times out and you make a API call without Authenticating (Authenticate=true) you will get an exception.

b.      This is the most efficient way of handling connections, providing you have session state.

 

SmartObjectManagementServer _mgmtServer = new SmartObjectManagementServer();

            SmartObjectClientServer _clientServer = new SmartObjectClientServer();

 

            string _conString = "Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=blackpearl;Port=5555";

 

            SCConnectionStringBuilder _connBuilder = new SCConnectionStringBuilder(_conString);

 

            _mgmtServer.CreateConnection();

            _clientServer.Connection = _mgmtServer.Connection;

 

            _mgmtServer.Connection.Open(_connBuilder.ToString());

            string sessionID = _mgmtServer.Connection.GetResumableSessionCookie();

 

            SmartObjectExplorer smoExplorer = _mgmtServer.GetSmartObjects(SmartObjectInfoType.System);

            _mgmtServer.Connection.Close();

 

            //Time elapse...

 

            SmartObject smo = null;

            try

            {

                _connBuilder.Authenticate = false;

                _clientServer.Connection.Open(_connBuilder.ToString());

                _clientServer.Connection.ResumeSession(sessionID);

                smo = _clientServer.GetSmartObject(smoExplorer.SmartObjects["UMUser"].Guid);

                _clientServer.Connection.Close();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }

 

            //Some more time elapse...

 

            if (smo != null)

            {

                SmartObjectList userList;

                try

                {

                    smo.MethodToExecute = "Get_Users";

                    smo.ListMethods[smo.MethodToExecute].Parameters["Label_Name"].Value = "k2";

                    _clientServer.Connection.Open(_connBuilder.ToString());

                    userList = _clientServer.ExecuteList(smo);

                    _clientServer.Connection.EndSession(sessionID);

                    _clientServer.Connection.Close();

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.Message);

                }

            }

 

Happy blackpearling!