Hussien.SnowflakeId 3.3.0

dotnet add package Hussien.SnowflakeId --version 3.3.0
                    
NuGet\Install-Package Hussien.SnowflakeId -Version 3.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hussien.SnowflakeId" Version="3.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hussien.SnowflakeId" Version="3.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Hussien.SnowflakeId" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hussien.SnowflakeId --version 3.3.0
                    
#r "nuget: Hussien.SnowflakeId, 3.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hussien.SnowflakeId@3.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hussien.SnowflakeId&version=3.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Hussien.SnowflakeId&version=3.3.0
                    
Install as a Cake Tool

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 NuGet Nuget

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: image


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:

image

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.3.0 367 12/12/2025
3.2.0 483 10/10/2025
3.1.0 335 4/1/2025
3.0.1 369 11/15/2024
3.0.0 3,585 11/24/2023
2.1.0 405 4/29/2023
2.0.0 497 1/5/2023