After investing some time, I managed a library for it; I called it Azure AutoNumber. The project is using the latest version of Azure NuGet packages, and it supports .NET Standard 2.0 and 2.1.

How to use AutoNumber

First, Install it via NuGet:

dotnet add package AzureAutoNumber --version 1.0.0

For generating ids, you need to make a singleton instance of UniqueIdGenerator class. This class has a dependency on BlobOptimisitcDataStorage class.

var blobStorageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(connectionString); var blobOptimisticDataStore = new BlobOptimisticDataStore(blobStorageAccount, "unique-ids"); var idGenerator = new UniqueIdGenerator(blobOptimisticDataStore);

BlobOptimisitcDataStorage will create a new container called “unique-ids” on your blob storage. As I said earlier, the instance of UniqueIdGenerator should be a singleton in every App Domain.

Now by calling the NextId method, it is easy to generate ids. The NextId method requires a parameter called ScopeName. By ScopeName, you can create different ids for different scopes.

var orderId = idGenerator.NextId("orders");

var invoiceId = idGenerator.NextId("invoices");

If you prefer to register IUniqueIdGenerator and its dependencies in Microsoft Dependency Injection, you can use the service extension of the package like the following example.

// configure the services

services.AddAutoNumber(); // Inject `IUniqueIdGenerator` in constructor public class Foo

{

public Foo(IUniqueIdGenerator idGenerator)

{

_idGenerator = idGenerator;

}

}