简体   繁体   中英

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

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;
    }

在此处输入图像描述

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.

在此处输入图像描述

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.

在此处输入图像描述

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM