private static void ensureValidOptions(MongoDistributedCacheOptions options) { if (string.IsNullOrEmpty(options.Collection)) { throw new ArgumentException($"{nameof(options.Collection)} is null or empty!"); } if (string.IsNullOrEmpty(options.Database)) { throw new ArgumentException($"{nameof(options.Database)} is null or empty!"); } if (options.Hosts == null || options.Hosts.Count < 1) { throw new ArgumentException($"{nameof(options.Hosts)} must have at least one host!"); } }
public static IServiceCollection AddMongoDistributedCache(this IServiceCollection services, MongoDistributedCacheOptions options) { if (services == null) { throw new ArgumentNullException(nameof(services)); } ensureValidOptions(options); services.AddOptions(); services.Configure <MongoDistributedCacheOptions>(m => { m.Username = options.Username; m.Password = options.Password; m.Database = options.Database; m.Collection = options.Collection; m.ExpiredRemovalInterval = options.ExpiredRemovalInterval; m.Hosts = options.Hosts; }); services.AddSingleton <IMongoAccessor, MongoAccessor>(); services.TryAdd(ServiceDescriptor.Singleton <IDistributedCache, MongoDistributedCache>()); return(services); }