Hussien.SnowflakeId
3.3.0
dotnet add package Hussien.SnowflakeId --version 3.3.0
NuGet\Install-Package Hussien.SnowflakeId -Version 3.3.0
<PackageReference Include="Hussien.SnowflakeId" Version="3.3.0" />
<PackageVersion Include="Hussien.SnowflakeId" Version="3.3.0" />
<PackageReference Include="Hussien.SnowflakeId" />
paket add Hussien.SnowflakeId --version 3.3.0
#r "nuget: Hussien.SnowflakeId, 3.3.0"
#:package Hussien.SnowflakeId@3.3.0
#addin nuget:?package=Hussien.SnowflakeId&version=3.3.0
#tool nuget:?package=Hussien.SnowflakeId&version=3.3.0
SnowflakeId
This is an implementation of snowflakeId algorithm in C# programming language, this algorithm is developed by X formally (Twitter)
Get Started
Hussien.SnowflakeId is a library that can help you to generate a unique Id, specifically for those who are working in a Distributed Systems. The current version of this library is 3.3.0, and it supports net6.0, net7.0, net8.0, net9.0 and net10.0.
To strat using Hussien.SnowflakeId library you can install it by using Nugget Mackage Manger or by installing it from the command line via dotnet cli by running the following command:
dotnet add package Hussien.SnowflakeId --version 3.3.0
| Package | Varsion | Downloads |
|---|---|---|
Hussien.SnowflakeId |
With Asp.Net Core Application
First you have to imports these namespaces
using SnowflakeId.Core;
using SnowflakeId.DependencyInjection;
Second register the Hussine.SnowflakeId library service by adding next two lines of code:
builder.Services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1; // in production get this value from the appsettings.json file.
options.UseConsoleLog = true; // this is available only on version 3.1.0 and above.
});
The Complete Asp.NET Core Example:
using Microsoft.AspNetCore.Builder;
using SnowflakeId.Core;
using SnowflakeId.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1; // in production get this value from the appsettings.json file.
options.UseConsoleLog = false; // this is available only on version 3.1.0 and above.
});
var app = builder.Build();
app.MapGet("/", (ISnowflakeService snowflakeService) =>
{
long generatingId = snowflakeService.GenerateSnowflakeId();
DateTime generatedAt = snowflakeService.GetGeneratedDateTimeBySnowflakeId(generatingId);
int dataCenterId = snowflakeService.GetDataCenterIdBySnowflakId(generatingId);
return $"The genrated Id is: { generatingId } - and is genrated at: { generatedAt } - at Data Center Id: {dataCenterId}";
});
app.Run();
And here is the result if you run the app:
With Console Application Or any other .NET application:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SnowflakeId.Core;
using SnowflakeId.DependencyInjection;
using System;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 7;
options.UseConsoleLog = false; // this is available only on version 3.1.0 and above.
// These are two new events are added to version 3.2.0,
// one is firing before generating Id and the other one after generating id.
options.Events = new SnowflakeIdEvents
{
OnCreatedSnowflakeId = context =>
{
Console.WriteLine("OnCreatedSnowflakeId --> The Id is: {0} and is generated At: {1}", context.Id, context.GeneratedDateTime);
return Task.CompletedTask;
},
OnCreatingSnowflakeId = context =>
{
Console.WriteLine("OnCreatingSnowflakeId --> Generating Id at data center has id: {0}", context.DataCenterId);
return Task.CompletedTask;
}
};
});
}).Build();
var idServive = host.Services.GetRequiredService<ISnowflakeService>();
var uniqueId = idServive.GenerateSnowflakeId();
Console.WriteLine("The unique Id is: {0}", uniqueId);
Console.WriteLine("*******************************");
var generatedAt = idServive.GetGeneratedDateTimeBySnowflakeId(uniqueId);
Console.WriteLine("The Id is: {0} and is generated At: {1}", uniqueId, generatedAt);
var dataCenterId = idServive.GetDataCenterIdBySnowflakId(uniqueId);
Console.WriteLine("The id is generated at data center has id: {0}", dataCenterId);
Console.ReadLine();
As you can see from the previous code, you can generate a new id, then you can query at which time the id is generated, and lastly at which data center id is saved . See the result of the above console application in the below pic:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.0)
-
net6.0
- Microsoft.Extensions.Configuration.Abstractions (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 6.0.0)
-
net7.0
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 7.0.0)
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.