Create a unique stream number

I need to create a unique sequence number from multiple threads. I created a simple class below and it seems to work, but I'm not sure if I can rely on a unique sequence number.

In addition, I need the number to return to 0 if it exceeds 999999. I do not expect it to roll over for a very long time, as the method will probably be called less than 100 times a day. I know that the system will be periodically shut down for maintenance before it has a chance to reach 999999.

The method GetSequenceNumberwould be called from the xslt transform on the BizTalk map, and this method could be called more than once at a time (within the same BizTalk host instance).

On my dev system, it works correctly and generates different values, even if BizTalk calls the method more than once at the same time. There is only one BizTalk host instance on the Dev system.

However, there are two servers in the production system. Do I believe that this method cannot guarantee uniqueness between servers, since they work in different application domains?

I cannot use guid because the sequence number is limited to 16 characters.

public class HelperMethods
{
    private static int sequenceNumber = 0;

    public string GetSequenceNumber()
    {
        string result = null;

        int seqNo = Interlocked.Increment(ref sequenceNumber);

        result = string.Format("{0:MMddHHmmss}{1:000000}", DateTime.Now, seqNo);

        return result;
    }
}

I thought I could use the computer name on the server and add some arbitrary character, so that even if the serial number generated on one server was the same as the other, it would still be different, but I'm not sure how much that would be unique. Something like that:

    string seqNumber = (MachineName == "Blah" ? "A" : "B") + GetSequenceNumber();

- - , ? , . , reset 0, 1000000 ?

+4
3

16 . Guid. Guid 24 Convert.ToBase64String, , XOR, , 16 .

Guid gd = Guid.NewGuid();

byte[] ba = gd.ToByteArray();

ba = ba.Zip(ba.Reverse(), (b0, b1) => (byte)(b0 ^ b1)).ToArray();

string mostLikelyUnique16 = Convert.ToBase64String(ba).Substring(0, 16);

8QBIi7JpCeHhCWmy.

, , , , .

1 - .

+3

, " ", . , , 10 3000000-3999999. 100 4200000-4299999.

- , .

+2

.

BizTalk AppDomains, Orchestration AppDomain . - xor, - - ( ), . , - .

BizTalk , , . GUID ( ), . , . ( BizTalk, , - , WCF SQL Server SEQUENCE), ( , ).

, ?

  • . , - , , GUID (, , -, GUID, ).
  • , singleton orchestration ( ). , - , , ; , , ; SQL (2012+) SEQUENCE, .
  • . ? , ?
+1

Source: https://habr.com/ru/post/1673359/


All Articles