Using Dictionary for BizTalk Instances

Sometimes in BizTalk, we may need to use Dictionary for checking various conditions for multiple messages/Records of the same BizTalk Instance. This can be achieved using Dictionary ThreadStaticAttribute.

Requirement: My BizTalk Orchestration accepts envelop message (Multiple records), I need to loop through each record and insert the data in sql table. Here the condition is I should not insert duplicate records in the table as my envelope message might have duplicate records. So, in orchestration before sql insert, I need to check whether the record is already inserted.

This requirement can be achieved using Dictionary ThreadStaticAttribute.

ThreadStatic variable is scoped to the current thread that is being processed.

We need to call a .net component from BizTalk expression shape by sending Key and Value pair as input parameters.

 

Dictionary.png

 

//SampleLookup is variable name which points to the class “LookUpHelper”

//Lookup is the name of the method which is accepting two parameters.

// lookupResult is Boolen variable.

//Key, Value is some unique identification for the record, example PolicyId/BrokerId…

 

Below .net component has a method “Lookup” which accepts two input parameters and return Boolean.

  • If the record exists in Dictionary, method will return “true”.
  • If the record doesn’t exist in Dictionary, method will insert the data into Dictionary and return “false”.
  • In Orchestration, If the method return value is “true”, you can skip sql insertion.
  • If the method return value is “false”, you can proceed for sql insertion.

You need to call the lookup method for each record in loop shape.

//.Net Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace SampleNamespace

{

    [Serializable]

    public class LookUpHelper

    {

        //Create Dictionary

        [System.ThreadStaticAttribute]

        private static System.Collections.Generic.Dictionary<string, string> _data = new System.Collections.Generic.Dictionary<string, string>();

        public static System.Collections.Generic.Dictionary<string, string> Data

        {

            get

            {

                if (_data == null)

                {

                    _data = new System.Collections.Generic.Dictionary<string, string>();

                }

                return _data;

            }

            set

            {

                _data = value;

            }

        }

            /// <summary>

        /// Method to check if the value is already present and if not, store the value in dictionary against a key

        /// </summary>

        /// <param name=”key”>key</param>

        /// <param name=”value”>value</param>

        public Boolean Lookup(string key, string value)

        {

            if (Data.ContainsKey(key))

            {              

                return true;

            }

            else

            {               

                Data.Add(key, value);

                return false;

            }

        }

    }

}

Advertisements

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s