C# Programmatically Create BizTalk Host, Host Instances and Set Orchestration (Xlang) and Messaging Polling Interval (Performance Tuning)

Recently we were getting too many SQL timeout issues in our BizTalk environment. After multiple troubleshooting one of the performance optimization suggestion was to increase the Polling Interval for all the host instances to decrease the load on SQL Server.

Polling interval of Host means how frequently it polls the Messaging(Send & Receive) or Orchestration (Xlang) Host queue to check for new messages as Subscriber.

In Low Latency scenarios, users prefer to set this value to very low up to 50ms, but this definitely puts extra load on SQL Server. This situation can be even worsened in the case of a large BizTalk Group or with many host instances.

The polling interval of a host is of 2 types and has a default value of 500ms

  • Messaging – Polling interval for messaging(send & receive) host queues
  • Orchestration – Polling Interval for Orchestration (Xlang) host queue
BizTalk Host – Polling Interval of two types Messaging and Orchestration, default value as 500ms

As a best practice to reduce the load on SQL Server it’s recommended to set a very high value of Messaging Polling interval for dedicated Process Hosts – running Orchestration, because it’s just running the orchestration and not Receive or Send Port.

Similarly, for dedicated Send Port hosts, it’s recommended to set the Orchestrations polling interval to a very high value.

For Receive Port(One-Way) hosts it is recommended to increase both Messaging and Orchestration polling interval to a very high value because Receive Locations acts as Publisher and not Subscriber to pick new messages.

To disable polling, you can set the polling interval to a very big number as listed in the table.

Server HostsMessagingOrchestration
ReceiveHost

Because we are only publishing incoming messages to the BizTalk message box through a one-way receive location, polling is not required on the ReceiveHost.
200000200000
TransmitHost

Because we are only receiving messaging instances from the BizTalk message box, orchestration polling is not required on the TransmitHost (send host).
50 – Low Latency/
500
200000
ProcessHost

Because we are only receiving orchestration instances from the BizTalk message box, messaging polling is not required on the ProcessHost (Processing host).
20000050 – Low Latency/
500

You can set this value from BizTalk Admin Console as well –

Use the Settings Dashboard to configure the polling intervals of a given host, across the BizTalk Group.

  1. In the BizTalk Server Administration Console, expand BizTalk Server Administration, right-click BizTalk Group, and then click Settings.
  2. In the BizTalk Settings Dashboard dialog box, on the Hosts page, on the General tab, under Polling Intervals, you will find the Messaging and Orchestration values. By default, both these values are set to 500 milliseconds.

In my case, we needed this activity for all host instances so we decided to achieve it programmatically using C# console app.

It talks about other Host related operations like – Creating Host & Host Instances, Update Host Instances, Delete Host Instances and most importantly changing the Polling Interval settings.

Find the code piece for all these activities –

Create Host –

public static void CreateHost(
string HostName, int HostType, string NTGroupName, bool AuthTrusted)
    {
        try
        {
            PutOptions options = new PutOptions();
            options.Type = PutType.CreateOnly;

            //create a ManagementClass object and spawn a ManagementObject instance  
            ManagementClass objHostSettingClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_HostSetting", null);
            ManagementObject objHostSetting = objHostSettingClass.CreateInstance();

            //set the properties for the Managementobject  
            objHostSetting["Name"] = HostName;
            objHostSetting["HostType"] = HostType;
            objHostSetting["NTGroupName"] = NTGroupName;
            objHostSetting["AuthTrusted"] = AuthTrusted;

            //create the Managementobject  
            objHostSetting.Put(options);
            System.Console.WriteLine("Host - " + HostName + " - has been created successfully");
        }
        catch (Exception excep)
        {
            System.Console.WriteLine("CreateHost - " + HostName + " - failed: " + excep.Message);
        }
    }

Create Host Instance –

<code>public static void CreateHostInstanceMapAndInstall(
string hostName, string svrName, string uid, string pwd)
 {
            try
            {
                //Build the name of the HostInstance - name has to be in the below format  
                string hostInstanceName = "Microsoft BizTalk Server" //Name of product  
                                  + " " + hostName         //Name of Host of which instance is to be created  
                                  + " " + svrName;         //Name of Server on which instance is to be created  

                //Create an instance of the ServerHost class using the System.Management namespace  
                ObjectGetOptions svrHostOptions = new ObjectGetOptions();
                ManagementClass svrHostClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_ServerHost", svrHostOptions);
                ManagementObject svrHostObject = svrHostClass.CreateInstance();

                //Set the properties of the ServerHost instance  
                svrHostObject["ServerName"] = svrName;
                svrHostObject["HostName"] = hostName;

                //Invoke the Map method of the ServerHost instance  
                svrHostObject.InvokeMethod("Map", null);

                //Create an instance of the HostInstance class using the System.Management namespace  
                ObjectGetOptions hostInstOptions = new ObjectGetOptions();
                ManagementClass hostInstClass = new ManagementClass("root\\MicrosoftBizTalkServer", "MSBTS_HostInstance", hostInstOptions);
                ManagementObject hostInstObject = hostInstClass.CreateInstance();

                //Set the properties of the HostInstance class  
                hostInstObject["Name"] = hostInstanceName;

                //Build a parameter array  
                object[] args = new object[2];
                args[0] = uid;
                args[1] = pwd;

                //Invoke the Install method of the HostInstance  
                hostInstObject.InvokeMethod("Install", args);

                Console.WriteLine("HostInstance was mapped and installed successfully. Mapping created between Host: " + hostName + " and Server: " + svrName);
                return;
            }
            catch (Exception excep)
            {
                Console.WriteLine("Failure during HostInstance creation: " + excep.Message);
            }
        }</code>

Update Polling Interval –

<code>public static void updatePollingInterval()
        {
            try
            {
                //Create EnumerationOptions and run wql query  
                EnumerationOptions enumOptions = new EnumerationOptions();
                enumOptions.ReturnImmediately = false;

                //Search for all HostInstances of 'InProcess' type in the Biztalk namespace scope  
                ManagementObjectSearcher searchObject = new ManagementObjectSearcher("root\\MicrosoftBizTalkServer", "Select * from MSBTS_HostSetting", enumOptions);

                //Enumerate through the result set and start each HostInstance if it is already stopped  
                foreach (ManagementObject inst in searchObject.Get())
                {
                    bool boolUpdateRequired = false;
                    //Orchestration Hosts
                    if (inst["Name"].ToString().ToUpper().Contains(xlangHostNameFilter))
                    {
                        inst["MessagingMaxReceiveInterval"] = messagingPollingInterval;
                        Console.WriteLine("HostName - " + inst["Name"].ToString() + " MessagingMaxReceiveInterval updated");
                        boolUpdateRequired = true;
                    }
                    //Transmit Hosts
                    else if (inst["Name"].ToString().ToUpper().Contains(transmitHostNameFilter))
                    {
                        inst["XlangMaxReceiveInterval"] = xlangPollingInterval;
                        Console.WriteLine("HostName - " + inst["Name"].ToString() + " XlangMaxReceiveInterval updated");
                        boolUpdateRequired = true;
                    }

                    //Receive Hosts
                    else if (inst["Name"].ToString().ToUpper().Contains(receiveHostNameFilter))
                    {
                        inst["XlangMaxReceiveInterval"] = xlangPollingInterval;
                        Console.WriteLine("HostName - " + inst["Name"].ToString() + " XlangMaxReceiveInterval updated");

                        //Setting Messaging Receive Interval for One Way Host Instances 
                        if (!
                            (inst["Name"].ToString().ToUpper().Contains("WCF") ||
                            inst["Name"].ToString().ToUpper().Contains("JMS") ||
                            inst["Name"].ToString().ToUpper().Contains("SOAP") ||
                            inst["Name"].ToString().ToUpper().Contains("HTTP") ||
                            inst["Name"].ToString().ToUpper().Contains("GENERIC_RECEIVEHOST")
                            ))
                        {
                            inst["MessagingMaxReceiveInterval"] = messagingPollingInterval;
                            Console.WriteLine("HostName - " + inst["Name"].ToString() + " MessagingMaxReceiveInterval updated");
                        }
                        boolUpdateRequired = true;
                    }

                    //Tracking Host
                    else if (inst["Name"].ToString().ToUpper().Contains(trackingHostNameFilter))
                    {
                        inst["XlangMaxReceiveInterval"] = xlangPollingInterval;
                        Console.WriteLine("HostName - " + inst["Name"].ToString() + " XlangMaxReceiveInterval updated");
                        inst["MessagingMaxReceiveInterval"] = messagingPollingInterval;
                        Console.WriteLine("HostName - " + inst["Name"].ToString() + " MessagingMaxReceiveInterval updated");
                        boolUpdateRequired = true;
                    }
                    if (System.Convert.ToBoolean(ConfigurationManager.AppSettings["updatePollingInterval"]))
                    {
                        //Update Max Polling Interval
                        if (boolUpdateRequired)
                        {
                            //update the ManagementObject  
                            PutOptions options = new PutOptions();
                            options.Type = PutType.UpdateOnly;
                            inst.Put(options);
                        }
                    }
                }
            }
            catch (Exception excep)
            {
                Console.WriteLine("Function-updatePollingInterval- Failure while updating Polling Interval: " + excep.Message);
            }
        }</code>

Delete Host and Host Instances –

<code>public static void DeleteHost(string HostName)
        {
            try
            {
                ManagementObject objHostSetting = new ManagementObject();
                objHostSetting.Scope = new ManagementScope("root\\MicrosoftBizTalkServer");

                //define lookup query  
                string strQuery = "MSBTS_HostSetting.Name='" + HostName + "'";
                objHostSetting.Path = new ManagementPath(strQuery);

                //delete the Managementobject  
                objHostSetting.Delete();

                System.Console.WriteLine("Host - " + HostName + " - has been deleted successfully");
            }
            catch (Exception excep)
            {
                System.Console.WriteLine("DeleteHost - " + HostName + " - failed: " + excep.Message);
            }
        }</code>

Update Host –

public static void UpdateHost(string HostName, string NTGroupName, bool AuthTrusted)
        {
            try
            {
                PutOptions options = new PutOptions();
                options.Type = PutType.UpdateOnly;

                ManagementObject objHostSetting = new ManagementObject();
                objHostSetting.Scope = new ManagementScope("root\\MicrosoftBizTalkServer");

                //define lookup query  
                string strQuery = "MSBTS_HostSetting.Name='" + HostName + "'";
                objHostSetting.Path = new ManagementPath(strQuery);

                //redefine properties value  
                objHostSetting["NTGroupName"] = NTGroupName;
                objHostSetting["AuthTrusted"] = AuthTrusted;

                //update the ManagementObject  
                objHostSetting.Put(options);
                System.Console.WriteLine("Host - " + HostName + " - has been updated successfully");
            }
            catch (Exception excep)
            {
                System.Console.WriteLine("UpdateHost - " + HostName + " - failed: " + excep.Message);
            }
        }

Hope it’s helpful.

Download the complete code from here

Contact Me:- 

@Gmail, @Facebook , @Twitter, @LinkedIn , @MSDNTechnet, @My Personal Blog 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s