using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using System; using System.Collections.Generic; using System.Text; namespace TEAMModelOS.SDK.DI { public static class SnowflakeIDExtensions { /// /// 分散式ID產生器 (Snowflake算法,支援線程安全) /// /// 數據中心ID (0~31) /// 工作機器ID (0~31) public static IServiceCollection AddSnowflakeId(this IServiceCollection services, long datacenterId, long workerId) { if (services == null) throw new ArgumentNullException(nameof(services)); if (datacenterId > 31 || datacenterId < 0) throw new Exception("datacenterId only 0-31"); if (workerId > 31 || workerId < 0) throw new Exception("workerId only 0-31"); services.TryAddSingleton(); services.Configure(o => { o.DatacenterId = datacenterId; o.WorkerId = workerId; }); return services; } } }