public async Task InitializeAsync() { using IServiceScope scope = this.provider.CreateScope(); using BloonContext db = scope.ServiceProvider.GetRequiredService <BloonContext>(); List <FeatureStatus> featureStatuses = await db.FeatureStatuses .ToListAsync(); foreach (Type type in Assembly.GetEntryAssembly() .GetTypes() .Where(t => typeof(Feature).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract) .OrderBy(t => t.Name)) { Feature feature = ActivatorUtilities.CreateInstance(this.provider, type) as Feature; if (this.features.Any(f => f.Name == feature.Name)) { throw new DuplicateNameException($"Duplicate feature name \"{feature.Name}\" for \"{type.Name}\"!"); } FeatureStatus featureStatus = featureStatuses.Where(m => m.Name == feature.Name).FirstOrDefault(); if (featureStatus == null) { featureStatus = new FeatureStatus { Name = feature.Name, Enabled = true, }; if (!feature.Protected) { db.FeatureStatuses.Add(featureStatus); } } Log.Information("Loaded feature {0} - {1}", feature.Name.PadRight(32), featureStatus.Enabled); await feature.Initialize(); if (featureStatus.Enabled) { this.featuresToEnable.Add(feature); } this.features.Add(feature); } await db.SaveChangesAsync(); }
public async Task UpdateFeatureStatusAsync(string featureName, bool enabled) { using IServiceScope scope = this.provider.CreateScope(); using BloonContext db = scope.ServiceProvider.GetRequiredService <BloonContext>(); FeatureStatus featureStatus = await db.FeatureStatuses .Where(m => m.Name == featureName) .FirstOrDefaultAsync(); if (featureStatus == null) { featureStatus = new FeatureStatus { Name = featureName, }; db.FeatureStatuses.Add(featureStatus); } featureStatus.Enabled = enabled; await db.SaveChangesAsync(); }