Thursday, May 18, 2017

AX 2012 Business Connector Sessions

Ax business connector provides a maximum of 2000 simultaneous sessions (web users). However, the online users sometimes are more than the 2000 limit, forcing further connections to queue and wait for other sessions to end. To go around this, you can create multiple AOS and point to the same database. While connecting, chose an AOS randomly. In addition to that, load-balancing will come in handy. Below is a sample C# code that connects using the business connector. FEEL FREE TO SUGGEST SOME IMPROVEMENTS



        public AXConnector()
        {
            this.axapter = new BCN.Axapta();
            bool ErrorOccured = true;
            int connectionNo;

            RestartConnection:
            connectionNo = new Random(DateTime.Now.Millisecond).Next(1, 4);

            //1st trial
            if (ErrorOccured && connectionNo==1)
            {
                try
                {
                    aos = "AOS1@AXSERVER1:2712";
                    this.axapter.LogonAs(this.strUserName, domain,
                        new NetworkCredential(this.strUserName, this.password, domain),
                        company, "en-us", aos, "");

                }
                catch (Exception e)
                {
                    ErrorOccured = true;
                    e.Data.Clear();
                    goto RestartConnection;
                }
            }

            //2nd trial
            if (ErrorOccured && connectionNo == 2)
            {
                ErrorOccured = false;
                try
                {
                    this.axapter = new BCN.Axapta();

                    aos = "AOS2@AXSERVER1:2713";
                    this.axapter.LogonAs(this.strUserName, domain,
                        new NetworkCredential(this.strUserName, this.password, domain),
                        company, "en-us", aos, "");

                }
                catch (Exception e)
                {
                    ErrorOccured = true;
                    e.Data.Clear();
                    goto RestartConnection;

                }
            }

            //3rd trial
            if (ErrorOccured && connectionNo == 3)
            {
                ErrorOccured = false;
                try
                {
                    this.axapter = new BCN.Axapta();

                    aos = "AOS3@AXSERVER1:2714";
                    this.axapter.LogonAs(this.strUserName, domain,
                        new NetworkCredential(this.strUserName, this.password, domain),
                        company, "en-us", aos, "");

                }
                catch (Exception e)
                {
                    ErrorOccured = true;
                    e.Data.Clear();
                    goto RestartConnection;
                }
            }

            //4th trial
            if (ErrorOccured && connectionNo == 4)
            {
                ErrorOccured = false;
                try
                {
                    this.axapter = new BCN.Axapta();

                    aos = "AOS4@AXSERVER1:2715";
                    this.axapter.LogonAs(this.strUserName, domain,
                        new NetworkCredential(this.strUserName, this.password, domain),
                        company, "en-us", aos, "");

                }
                catch (Exception e)
                {
                    ErrorOccured = true;
                    e.Data.Clear();
                    goto RestartConnection;
                }
            }            
        }

Wednesday, February 4, 2015

/*
AIF vs Business Connector
AIF is terriblly slow as I have come to realise practically (Note: both have been tested with razor and traditional aspx), go for BC, use the code below to do your "thing"
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;

using BCN = Microsoft.Dynamics.BusinessConnectorNet;

namespace XXXXX.Controllers
{
    public partial class AXConnector:IDisposable
    {
        protected BCN.Axapta ax;
        public string ConnectorClass;
        protected string strUserName = "abc", password = "xyz";

        public AXConnector(string _connectorClass = "YourClassHapa")
        {
            try
            {
                this.ConnectorClass = _connectorClass;

                this.ax = new BCN.Axapta();
                //ax.Logon(null, null, null, null);
                this.ax.LogonAs(this.strUserName.Trim(), "", new NetworkCredential(this.strUserName, this.password), "", "", "", "");
            }
            catch (Exception e)
            {
                throw;
            }
        }

        public BCN.Axapta GetAxapta
        {
            get
            {
                return this.ax;
            }
        }

        public BCN.AxaptaRecord Execute(string axTableName, string sqlStatement = "select forupdate * from %1")
        {
            #region hapa ndio vile unatumia the axRecord in your calling-methods

            //BCN.AxaptaRecord axRecord = this.GetAxapta.CreateAxaptaRecord(axTableName);

            //try
            //{
            //    using (axRecord)
            //    {
            //        axRecord.ExecuteStmt(sqlStatement);

            //        while (axRecord.Found)
            //        {
            //            #region Read
            //            axRecord.get_Field("ID");
            //            axRecord.get_Field("Value1");
            //            #endregion

            //            #region Write
            //            ax.TTSBegin();

            //            axRecord.set_Field("ID", "2");
            //            axRecord.set_Field("Value1", "Testing New");
            //            axRecord.Update();

            //            ax.TTSCommit();
            //            #endregion

            //            axRecord.Next();
            //        }
            //    }
            //}
            //catch (Exception e)
            //{
            //    throw;
            //}

            #endregion

            return this.GetAxapta.CreateAxaptaRecord(axTableName);
        }

        public void Dispose()
        {
            try
            {
                this.ax.Logoff();
                this.ax = null;
            }
            catch (Exception e)
            {
                e.Data.Clear();
            }
        }
    }
}
/*
change where you feel appropriate - GEUZA YOTE - happy coding
send your comments/inquiries to onyangofred@yahoo.com;onyangofred@gmail.com
*/