-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
68 lines (65 loc) · 2.52 KB
/
Program.cs
File metadata and controls
68 lines (65 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Tenon.DistributedId.Abstractions;
using Tenon.DistributedId.Abstractions.Extensions;
using Tenon.DistributedId.Snowflake;
using Tenon.DistributedId.Snowflake.Configurations;
using Tenon.Infra.Redis;
using Tenon.Infra.Redis.StackExchangeProvider;
namespace SnowflakeSample;
internal class Program
{
private static IServiceProvider _serviceProvider;
private static async Task Main(string[] args)
{
Console.WriteLine("Hello, World!");
try
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false)
.Build();
_serviceProvider = new ServiceCollection()
.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddConsole();
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
loggingBuilder.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss ";
});
})
.AddDistributedId(options =>
{
options.UseSnowflake(configuration.GetSection("DistributedId"));
options.UseWorkerNode<StackExchangeProvider>(configuration.GetSection("DistributedId")
.GetSection("WorkerNode"));
})
.BuildServiceProvider();
var idGenerator = new SnowflakeIdGenerator();
idGenerator.SetWorkerId(63);
Console.WriteLine(idGenerator.GetNextId());
using (var scope = _serviceProvider.CreateScope())
{
var iDGenerator = scope.ServiceProvider.GetService<IDGenerator>();
var redisProvider = scope.ServiceProvider.GetService<IRedisProvider>();
var workerNode = scope.ServiceProvider.GetService<WorkerNode>();
await workerNode?.RegisterAsync()!;
Console.WriteLine(iDGenerator.GetNextId());
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
Console.WriteLine("done");
Console.ReadLine();
}
}
}