Biztalk XLANG transform() output the same random value in a loop inside Biztalk orchestration

Viewed 57

I wrote a map to generate an HL7 message header (MSH). For the MSH.10 segment, by definition should be unique so I put the following in my map.

    public string MessageControlId()
    {
       //return System.DateTime.Now.ToString("yyyyMMddHHmmssffff");


       string firstPart = System.DateTime.Now.ToString("yyyyMMdd");
       string middlePart = new Random().Next( 1000, 9999 ).ToString();
       string lastPart = System.DateTime.Now.ToString("ffff");
        
       return firstPart + middlePart + lastPart;
    }

enter image description here

Then in my orchestration I call the header map multiple time in a loop. My goal is to generate multiple HL7 messages, each with its own message header and a unique MSH.10 value.

enter image description here

The code below is based on Microsoft Biztalk XLANG syntax which invokes the map to transform and create the message header via the transform() statement.

tMapType = System.Type.GetType(msgBre.HeaderMapName);

transform (msgHeader) = tMapType(msgBilling);

However, when I tested this out I see multiple HL7 message generated but many of them have duplicate value in term of their MSH.10 segment. I grouped them in different color below.

enter image description here

I expect separate value each time because in my code I generate a random number between 1000 and 9999. Plus I also generate the time value to the thousand of a second.

Do you know why this occur? My only theory is when I call the tranform() function, it does not really invoke the map to recreate the header each time...that seems wrong to me.

UPDATE:

Thanks to @hulihunskeli input, I was able to solve this by going into my orchestration in Biztalk and just prior of loop repetition I added a 200ms delay and that seems to solve it. I guess this is one of those things where the processing time of the loop is just too quick for the function to generate a new object which ensure a unique number.

enter image description here

1 Answers

You are using Random object which is a pseudo-random number generator, so it returns the same sequence of numbers with a same seed. You are not giving a seed explicitly to the constructor, so it uses default seed which is based on a system clock. If you are creating Random objects in a tight loop with a default seed then next() function will return you the same number multiple times, which I think is what happens here.

You should either give unique seed explicitly or use the same Random object all the time (if it is possible).

Related