public ConfigReloaderBackgroundService( IConfiguration configuration, AutoWireOptions autoWireOptions, ILogger <ConfigReloaderBackgroundService> logger ) { _configuration = configuration; _logger = logger; _interval = autoWireOptions.ReloadConfigurationInterval ?? TimeSpan.FromMinutes(1); }
/// <summary> /// Run the dependency and options wiring /// </summary> /// <param name="serviceCollection"></param> /// <param name="configuration"></param> /// <param name="options">Options for customizing the service discovery</param> /// <returns></returns> public static IServiceCollection AutoWire(this IServiceCollection serviceCollection, IConfiguration configuration, Action <AutoWireOptions> options) { var autoWireOptions = new AutoWireOptions(); ConfigurationBinder.Bind(configuration.GetSection("TGX.Extensions.DependencyInjection.AutoWire"), options); options?.Invoke(autoWireOptions); serviceCollection.AddOptions(); if (autoWireOptions.MakeOptionsMonitorUpdateSafe) { serviceCollection.AddSingleton(provider => new OptionsMonitorBindingExceptionNotifier(autoWireOptions.OnOptionsMonitorUpdateException)); serviceCollection.Add(ServiceDescriptor.Singleton(typeof(IOptionsMonitor <>), typeof(UpdateSafeOptionsMonitor <>))); } serviceCollection.AddSingleton(autoWireOptions); var assemblies = GetAssemblies(); var types = assemblies.SelectMany(x => x.GetExportedTypes()); if ((autoWireOptions.IncludePrefixed == null || !autoWireOptions.IncludePrefixed.Any()) && !autoWireOptions.RegisterByAttribute) { types = types.Where(x => autoWireOptions.EntryAssembly != null && autoWireOptions.EntryAssembly == x.Assembly || autoWireOptions.EntryAssembly == null && x.Assembly == Assembly.GetEntryAssembly()); } if (autoWireOptions.IncludePrefixed != null && autoWireOptions.IncludePrefixed.Any()) { types = types .Where(x => autoWireOptions.IncludePrefixed.Any(prefix => x.Namespace != null && x.Namespace.StartsWith(prefix))); } serviceCollection.RegisterServices(types, autoWireOptions); serviceCollection.RegisterOptions(types, configuration); if (autoWireOptions.ReloadConfiguration) { serviceCollection.AddHostedService <ConfigReloaderBackgroundService>(); } return(serviceCollection); }
private static IReadOnlyList <Type> GetTypes(IEnumerable <Type> types, AutoWireOptions options) { var candidateTypes = types .Where(x => x.GetCustomAttribute <IgnoreAutoWireAttribute>() == null && x.GetInterfaces().All(y => y.GetCustomAttribute <IgnoreAutoWireAttribute>() == null) && x.IsClass && !x.IsAbstract && !x.IsNested && !x.Name.EndsWith("Options", StringComparison.Ordinal) ); if (options.RegisterByAttribute) { candidateTypes = candidateTypes.Where(x => x.CustomAttributes.Any(y => y.AttributeType == typeof(AutoWireAttribute)) || x.GetInterfaces().Any(y => y.GetCustomAttribute <AutoWireAttribute>() != null)); } return(candidateTypes.ToList()); }
internal static void RegisterServices(this IServiceCollection serviceCollection, IEnumerable <Type> types, AutoWireOptions options) { var candidateTypes = GetTypes(types, options); var typesFromAutoWire = typeof(AutoWireAttribute).Assembly.GetExportedTypes(); foreach (var type in candidateTypes.Where(x => typesFromAutoWire.All(y => x != y))) { serviceCollection.TryAdd(ServiceDescriptor.Transient(type, type)); var interfaces = type.GetTypeInfo().ImplementedInterfaces.Where(x => x != typeof(IDisposable)); foreach (var @interface in interfaces) { if (serviceCollection.Any(y => y.ServiceType == @interface)) { continue; } if (options.RegisterByAttribute && @interface.GetCustomAttribute <AutoWireAttribute>() == null) { continue; } serviceCollection.TryAdd(new ServiceDescriptor(@interface, type, ServiceLifetime.Transient)); } } }