JArray CallApps(RbHeader rbh, string rbBodyString, string partitionId)
        {
            // Get App Master Info
            RbAppMasterCache rbappmc = GetAppMasterInfo(rbh);

            // Get App Routing Info
            RbAppRouterCache rbapprc = GetAppRoutingInfo(rbh);

            JArrayString ja_messagesString = null;
            JArray       ja_messages       = null;
            string       dllFilePath       = string.Empty;

            IAppRouterDll routedAppDll = null;
            Assembly      assembly     = null;

            // Load DLL from BLOB
            string baseDirectory            = string.Empty;
            string privateDllDirectory      = string.Empty;
            string cachedFileName           = string.Empty;
            string cachedFileNameWithoutExt = string.Empty;

            if (rbapprc.DevMode == "True")
            {
                string devdir = rbapprc.DevLocalDir;
                int    pos    = devdir.Length - 1;
                if (devdir.Substring(pos, 1) == @"\")
                {
                    dllFilePath = rbapprc.DevLocalDir + rbapprc.FileName;
                }
                else
                {
                    dllFilePath = rbapprc.DevLocalDir + @"\" + rbapprc.FileName;
                }

                baseDirectory            = Path.GetDirectoryName(dllFilePath);
                privateDllDirectory      = baseDirectory;
                cachedFileName           = Path.GetFileName(dllFilePath);
                cachedFileNameWithoutExt = Path.GetFileNameWithoutExtension(dllFilePath);
            }
            else
            {
                CachedDllFileInfo cachedDllFileInfo = null;
                lock (thisLock2)
                {
                    cachedDllFileInfo = CopyBlobToLocalDir(rbappmc, rbapprc, partitionId);
                }
                baseDirectory            = cachedDllFileInfo.BaseDirectory;
                privateDllDirectory      = cachedDllFileInfo.PrivateDllDirectory;
                cachedFileName           = Path.GetFileName(cachedDllFileInfo.PrivateDllFilePath);
                cachedFileNameWithoutExt = Path.GetFileNameWithoutExtension(cachedDllFileInfo.PrivateDllFilePath);
            }

            ////Static load without AppDomain
            //assembly = System.Reflection.Assembly.LoadFrom(dllFilePath);
            //routedAppDll = assembly.CreateInstance(rbapprc.ClassName) as IAppRouterDll;

            //Dynamic load using AppDomain
            try
            {
                string    appDomainName = appDomanNameBase + partitionId;
                AppDomain appDomain     = null;
                if (appDomainList.ContainsKey(partitionId))
                {
                    appDomain = appDomainList[partitionId];
                }

                if (appDomain == null)
                {
                    appDomain = CreateAppDomain(appDomainName, baseDirectory, privateDllDirectory);
                    lock (thisLock2)
                    {
                        appDomainList[partitionId] = appDomain;
                    }
                }
                routedAppDll = appDomain.CreateInstanceAndUnwrap(cachedFileNameWithoutExt, rbapprc.ClassName) as IAppRouterDll;
            }
            catch (Exception ex)
            {
                RbTraceLog.WriteError("E003", ex.ToString());
                ae = new ApplicationException("Error ** Exception occured during creating AppDomain & Instance(App DLL)");
                throw ae;
            }

            // ProcessMessage
            try
            {
                rbh.ProcessingStack = rbapprc.FileName;
                ja_messagesString   = routedAppDll.ProcessMessage(rbappmc, rbapprc, rbh, rbBodyString);
                ja_messages         = ja_messagesString.ConvertToJArray();
            }
            catch (Exception ex)
            {
                RbTraceLog.WriteError("E002", ex.ToString());
                ae = new ApplicationException("Error ** Exception occured in routed App DLL");
                throw ae;
            }

            return(ja_messages);
        }
        private void callAppButton_Click(object sender, EventArgs e)
        {
            if (textBoxDllFilePath.Text == string.Empty)
            {
                MessageBox.Show("** Error ** DLL File Local Path must be set !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (textBoxDeviceId.Text == string.Empty)
            {
                MessageBox.Show("** Error ** Device ID must be set !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (checkBoxSkipAppRouter.Checked && textBoxClassName.Text == string.Empty)
            {
                MessageBox.Show("** Error ** Class Name must be set !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // Save the TextBox content
            saveTextBoxContent();

            JObject jo_message = JsonConvert.DeserializeObject <JObject>(textBoxInput.Text);

            // Check RbHeader in detail
            RbHeaderBuilder hdBuilder = new RbHeaderBuilder(jo_message, textBoxDeviceId.Text);
            RbHeader        rbh       = hdBuilder.ValidateJsonSchema();
            // RbBody
            JObject jo_temp      = (JObject)jo_message[RbFormatType.RbBody];
            string  rbBodyString = JsonConvert.SerializeObject(jo_temp);

            // App Master Cache (RBFX.AppMaster)
            AppMaster        am      = new AppMaster(rbh.AppId, activeEncPassPhrase, activeSqlConnectionString, constTimeOutSec);
            RbAppMasterCache rbappmc = am.GetAppMaster();

            // App Router Cache (RBFX.AppRouting)
            RbAppRouterCache rbapprc;

            if (checkBoxSkipAppRouter.Checked)
            {
                rbapprc                 = new RbAppRouterCache();
                rbapprc.AppId           = rbh.AppId;
                rbapprc.AppProcessingId = rbh.AppProcessingId;
                rbapprc.ClassName       = textBoxClassName.Text;
                rbapprc.FileName        = textBoxDllFilePath.Text;
            }
            else
            {
                AppRouter ar = new AppRouter(rbh.AppId, rbh.AppProcessingId, activeSqlConnectionString, constTimeOutSec);
                rbapprc = ar.GetAppRouting();
            }

            // Load DLL
            //Assembly assembly = null;
            AppDomain     appDomain    = null;
            IAppRouterDll routedAppDll = null;

            try
            {
                //assembly = System.Reflection.Assembly.LoadFrom(textBoxDllFilePath.Text);
                //routedAppDll = assembly.CreateInstance(rbapprc.ClassName) as IAppRouterDll;
                string pid                      = Thread.CurrentThread.ManagedThreadId.ToString();
                string appDomainName            = "AppDomain_P" + pid;
                string cachedDirectory          = Path.GetDirectoryName(textBoxDllFilePath.Text);
                string cachedFileName           = Path.GetFileName(textBoxDllFilePath.Text);
                string cachedFileNameWithoutExt = Path.GetFileNameWithoutExtension(textBoxDllFilePath.Text);
                appDomain    = createAppDomain(appDomainName, cachedDirectory);
                routedAppDll = appDomain.CreateInstanceAndUnwrap(cachedFileNameWithoutExt, rbapprc.ClassName) as IAppRouterDll;
            }
            catch (Exception ex)
            {
                MessageBox.Show("** Application (DLL) Load Error ** Check File Path or Class Name \n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Process Message
            try
            {
                rbh.ProcessingStack = activeFileName;
                JArrayString ja_messagesString = routedAppDll.ProcessMessage(rbappmc, rbapprc, rbh, rbBodyString);
                JArray       ja_messages       = ja_messagesString.ConvertToJArray();
                textBoxOutput.Text = JsonConvert.SerializeObject(ja_messages);
                AppDomain.Unload(appDomain);
            }
            catch (Exception ex)
            {
                MessageBox.Show("** Error occured in Application (DLL) **\n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppDomain.Unload(appDomain);
                return;
            }
        }