Best Practise - K2 Host Server Connections

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!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值