Skip to content

Commit 6c42013

Browse files
committed
deterministic ids for all sales instances
1 parent 1121f60 commit 6c42013

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Solution/Sales/Program.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Linq;
3+
using System.Security.Cryptography;
4+
using System.Text;
35
using System.Threading.Tasks;
46
using NServiceBus;
57
using Shared;
@@ -15,21 +17,19 @@ static async Task Main(string[] args)
1517
LoggingUtils.ConfigureLogging("Sales");
1618

1719
var instanceName = args.FirstOrDefault();
18-
var instanceId = Guid.NewGuid();
1920

2021
if(string.IsNullOrEmpty(instanceName))
2122
{
2223
Console.Title = "Sales";
2324

2425
instanceName = "original-instance";
25-
instanceId = new Guid("14A46D23-4874-497B-ABCD-8D6E2488DB25");
2626
}
2727
else
2828
{
2929
Console.Title = $"Sales - {instanceName}";
3030
}
3131

32-
Console.WriteLine("Using instance-id {0}", instanceName);
32+
var instanceId = DeterministicGuid.Create("Sales", instanceName);
3333

3434
var endpointConfiguration = new EndpointConfiguration("Sales");
3535
endpointConfiguration.LimitMessageProcessingConcurrencyTo(4);
@@ -94,4 +94,19 @@ static void RunUserInterfaceLoop(SimulationEffects state, string instanceName)
9494
}
9595
}
9696
}
97+
98+
static class DeterministicGuid
99+
{
100+
public static Guid Create(params object[] data)
101+
{
102+
// use MD5 hash to get a 16-byte hash of the string
103+
using (var provider = new MD5CryptoServiceProvider())
104+
{
105+
var inputBytes = Encoding.Default.GetBytes(string.Concat(data));
106+
var hashBytes = provider.ComputeHash(inputBytes);
107+
// generate a guid from the hash:
108+
return new Guid(hashBytes);
109+
}
110+
}
111+
}
97112
}
512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)