Hereare some pointers when using the Connection object. This object is part of theBaseAPI from which each API inherit (SmartObjectClientServer,SmartObjectManagementServer etc).
Belowis some sample code based on three scenarios in which you can connectto the server from the API.
1. Scenario 1: Openingand closing multiple connections
a. This is the mostexpensive way of using connections and causes the most overhead. Reason beingthat after each Open method on the connection object, the call is authenticatedon the server and this is what cause the most overhead. This scenario is typicallyused in a stateless environment where user context does not exit between methodcalls 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 Eachoperation
//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 : Oneconnection performing many operations.
a. This is a much moreefficient way of using connections. You can share the connection object betweenAPI’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: Usingsessions.
a. If your calling clienthas state and can manage sessions, this is the way to use it with theconnection object. The session connection timeout on the server can be set. Itdoes however apply to the entire server which means all servers hosted onthat server. The default time is 20 minutes. If a session times out and youmake a API call without Authenticating (Authenticate=true) you will get anexception.
b. This is the mostefficient 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 (Exceptionex)
{
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 (Exceptionex)
{
Console.WriteLine(ex.Message);
}
}
Happyblackpearling!