High performance, distributed unique thread-safe id generator for Azure.
- Human-friendly generated ids (number)
- High performant and fast
- 100% guarantee that won't cause any duplicate ids
- Supports .NET 8.0 and .NET 10.0
- Modern Azure SDK (Azure.Storage.Blobs 12.27.0)
- Central Package Management
The project relies on Azure Blob Storage. AutoNumber package will generate ids by using a single text file on the Azure Blob Storage.
var blobServiceClient = new BlobServiceClient(connectionString);
var blobOptimisticDataStore = new BlobOptimisticDataStore(blobServiceClient, "unique-ids");
var idGen = new UniqueIdGenerator(blobOptimisticDataStore);
// generate ids with different scopes
var id = idGen.NextId("urls");
var id2 = idGen.NextId("orders");
The project has an extension method to add it and its dependencies to Microsoft ASP.NET DI.
Use options builder to configure the service, take into account the default settings will read from appsettings.json.
services.AddAutoNumber(Configuration, x =>
{
return x.UseContainerName("container-name")
.UseStorageAccount("connection-string-or-connection-string-name")
//.UseBlobServiceClient(blobServiceClient)
.SetBatchSize(10)
.SetMaxWriteAttempts(100)
.Options;
});
public class Foo
{
public Foo(IUniqueIdGenerator idGenerator)
{
_idGenerator = idGenerator;
}
}
These are default configuration for AutoNumber. These options can be set via appsettings.json.
{
"AutoNumber": {
"BatchSize": 50,
"MaxWriteAttempts": 25,
"StorageContainerName": "unique-urls"
}
}
- .NET 8.0 SDK or .NET 10.0 SDK
- Docker (for integration tests)
dotnet build --configuration Release# Run all tests
dotnet test --configuration Release
# Run only unit tests
dotnet test UnitTests/UnitTests.csproj --configuration Release
# Run integration tests (requires Docker)
dotnet test IntegrationTests/IntegrationTests.csproj --configuration Release- Unit Tests: xUnit 2.9.3
- Integration Tests: xUnit 2.9.3 with TestContainers.Azurite
- Mocking: NSubstitute 5.3.0
This project uses Central Package Management (CPM). All NuGet package versions are managed in Directory.Packages.props at the root level.
Support this project and me via PayPal
Most of the credits of this library goes to Tatham Oddie for making SnowMaker. I forked his work and made lots of changes to modernize it with the latest .NET versions and Azure SDK.