Wednesday, January 22, 2014

Programmatically Enable/Disable Receive pipeline


We can achieve this functionality using the  "Microsoft.BizTalk.ExplorerOM" component.

Add reference "Microsoft.BizTalk.ExplorerOM" component to your application.

BtsCatalogExplorer bceExplorer = new BtsCatalogExplorer();
//Edit the following connection string to point to the correct database and server
bceExplorer.ConnectionString = "Integrated Security=SSPI;database=BizTalkMgmtDb;server=localhost";

public bool SetReceiveLocationState(string receivePortName, bool stateValue)
{
            bool returnValue = false;
            try
            {
                ReceivePort receivePort = bceExplorer.ReceivePorts[receivePortName];
                receivePort.ReceiveLocations.Cast<ReceiveLocation>().ToList().ForEach(receiveLocation => receiveLocation.Enable = stateValue);

                bceExplorer.SaveChanges();
                returnValue = true;
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("SetReceiveLocationState", e.Message);
                bceExplorer.DiscardChanges();
                returnValue = false;
            }
            return returnValue;
        }

Programmatically (Dynamically) change the properties of a Receive Location


We can update the properties of a File adapter Receive Location using the Microsoft.BizTalk.ExplorerOM (from {BizTalkInstallation}\DeveloperTools) and I refereed useful article from MSDN for my development.

   We have a requirement to programmatically(dynamically) change the File Mask property of the File receive location.
   The following code only deals with a File Mask property that can be changed in the File receive location.
It accepts few things as input, we accept the port name, which receive location you want to change, file mask and it will change the file mask property in the receive location.


Add reference "Microsoft.BizTalk.ExplorerOM" component to your application.

I have written below as per the MSDN article and this trick didn't worked for me.

BtsCatalogExplorer bceExplorer = new BtsCatalogExplorer();
//Edit the following connection string to point to the correct database and server
bceExplorer.ConnectionString = "Integrated Security=SSPI;database=BizTalkMgmtDb;server=localhost";

public bool UpdateReceiveLocationFileMask(string receivePortName, string receiveLocationName, string fileMask)
{
            bool returnValue = false;
            string transportData = string.Empty;
            XmlDocument doc = null;
            XmlNode root = null;
            XmlNode fileMaskNode = null;
            try
            {
                if (!string.IsNullOrEmpty(fileMask))
                {
                    ReceivePort receivePort = bceExplorer.ReceivePorts[receivePortName];
                    ReceiveLocation receiveLocation = receivePort.ReceiveLocations.Cast<ReceiveLocation>().Where(item => item.Name == receiveLocationName).FirstOrDefault();

                    if (receiveLocation != null)
                    {                        
                        transportData = receiveLocation.TransportTypeData;
                        doc = new XmlDocument();
                        doc.LoadXml(transportData);
                        root = doc.DocumentElement;
                        fileMaskNode = root.SelectSingleNode("FileMask");

                        if (fileMaskNode != null)
                        {
                            fileMaskNode.InnerText = fileMask;
                            receiveLocation.TransportTypeData = doc.OuterXml;
                            bceExplorer.SaveChanges();
                            returnValue = true;
                        }
                        bceExplorer.SaveChanges();
                        returnValue = true;
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("UpdateReceiveLocationFileMask", e.Message);
                bceExplorer.DiscardChanges();
                returnValue = false;
            }

            return returnValue;


The above code didn't worked, i have used Address property of the receive location and this trick worked for me.

public bool UpdateReceiveLocationFileMask(string receivePortName, string receiveLocationName, string fileMask)
{
            bool returnValue = false;
            string transportData = string.Empty;
            XmlDocument doc = null;
            XmlNode root = null;
            XmlNode fileMaskNode = null;
            try
            {
                if (!string.IsNullOrEmpty(fileMask))
                {
                    ReceivePort receivePort = bceExplorer.ReceivePorts[receivePortName];
                    ReceiveLocation receiveLocation = receivePort.ReceiveLocations.Cast<ReceiveLocation>().Where(item => item.Name == receiveLocationName).FirstOrDefault();

                    if (receiveLocation != null)
                    {
                        receiveLocation.Address = Path.Combine(Path.GetDirectoryName(receiveLocation.Address), fileMask);                        
                        bceExplorer.SaveChanges();
                        returnValue = true;
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("UpdateReceiveLocationFileMask", e.Message);
                bceExplorer.DiscardChanges();
                returnValue = false;
            }

            return returnValue;
}

Friday, January 17, 2014

Property Promotion inside Orchestration


Properties get promoted within BizTalk at different places, Example: some of the properties gets promoted by Adapters, Pipelines Components, Messaging Engine, etc.

    In custom pipeline components, developers can use either IBaseMessageContext.Write to write context properties or IBaseMessageContext.Promote to promote context properties. The basic difference between writing and promoting properties are, properties that are written cannot be used for message routing, whereas properties that are promoted can be used for message routing.

Below is the sample for promoting properties within Orchestration is:
    MSG_OUT(ProjectNameSpace.PropertySchema.PropertyName) = "XXX"

If we look at the outgoing message context, the above prompted property will not promote and it would be in the "NotPromoted" state.


Please follow the below instructions to promote your property within the orchestration:

    In BizTalk we have a Correlation concept, basically which routes the response messages to the correct running orchestration instance that initiated the request message. Within Orchestration we use Correlation Set and Correlation type to archive this type of instance routing.

Correlation type is nothing but a set of Properties and Correlation Set is based on Correlation type and its a set of properties with specific values. When we "Initialize a Correlation set" within orchestration, the orchestration instance automatically promotes those properties in Correlation set into the message context.

We can use this mechanism to promote properties and there is no need to do a follow up of the Correlations sets we initialized, and it NOT going to create unnecessary subscriptions.

Thursday, January 9, 2014

Modify your configuration file

Add "System.Configuration" reference to your application


 ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();      
            configFileMap.ExeConfigFilename = @"{path}";

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);


            Console.WriteLine(config.AppSettings.Settings["YourKey"].Value);
            config.AppSettings.Settings["YourKey"].Value = DateTime.Now.Date.ToString("yyMMdd");
            config.Save();


Monday, January 6, 2014

Copy list to new list, while adding new value to list overwrites previous value in the old list

List<T> Constructor (IEnumerable<T>):
Initializes a new instance of the List<T> class that contains elements copied from the specified collection.

Sample:
            List<string> col1 = new List<string> { "R1", "R2", "R3" };
            List<string> col2 = col1;

            col2.Add("R4");

            Console.WriteLine("Col1 Count is:" + col1.Count);
            Console.WriteLine("Col2 Count is:" + col2.Count);
            Console.ReadLine();

The OutPut is:
Col1 Count is:4
Col2 Count is:4

            List<string> col1 = new List<string> { "R1", "R2", "R3" };
            List<string> col2 =  new List<string>(col1);

            col2.Add("R4");

            Console.WriteLine("Col1 Count is:" + col1.Count);
            Console.WriteLine("Col2 Count is:" + col2.Count);
            Console.ReadLine();

The OutPut is:
Col1 Count is:3
Col2 Count is:4