This is the implementation of twitter's snowflakeId algorithm in C# language
SnowflakeId is a library that can help you to generate a unique Id, specifically for those who are working in a Distributed Systems. The currently version is version 2.0.0, and there are break changes in this version with version 1.0.0 so be careful when you upgrade from version 1.0.0 to version 2.0.0
For any .NET application higher than .Net 6 install the library by using NuGet package command
dotnet add package Hussien.SnowflakeId --version 2.0.0
First you have to imports these namespaces
using SnowflakeId.Core;
using SnowflakeId.Core.DependencyInjection;
using SnowflakeId.Provider;
using SnowflakeIdResult = SnowflakeId.Provider.SnowflakeId;
Second register the Snowflake service by adding these lines:
builder.Services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1;
});
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1;
});
var app = builder.Build();
app.MapGet("/", (ISnowflakeService snowflakeService, ISnowflakeIdProvider snowflakeIdProvider) =>
{
long generatingId = snowflakeService.GenerateSnowflakeId();
SnowflakeIdResult sn = snowflakeIdProvider.GetDateTimeBySnowflakeId(generatingId);
return $"The genrated Id is: { generatingId } - and is genrated at { sn.GeneratedDateTime }";
});
app.Run();
And here is the result if you run the app:
using SnowflakeId.Core;
using SnowflakeId.Core.DependencyInjection;
using SnowflakeId.Provider;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1;
});
}).Build();
var idServive = host.Services.GetRequiredService<ISnowflakeService>();
var idProvider = host.Services.GetRequiredService<ISnowflakeIdProvider>();
var uniqueId = idServive.GenerateSnowflakeId();
var generatedAt = idProvider.GetDateTimeBySnowflakeId(uniqueId);
Console.WriteLine("The Id is: {0} and is generated At: {1}", generatedAt.Id, generatedAt.GeneratedDateTime);
Console.ReadLine();