/// <summary> /// Add an authorization service instance. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="services"></param> /// <param name="authorizationService"></param> /// <returns></returns> public static IServiceCollection AddAuthorization <T>( this IServiceCollection services, AuthorizationServiceOptionsBuilder builder) where T : class, IAuthorizationService { // Add the authorization manager instance. if (_authorizationManager == null) { _authorizationManager = new AuthorizationManager(); services.AddSingleton <IAuthorizationManager>(_authorizationManager); } // Register the service by interface. services.AddSingleton <IAuthorizationService, T>(); // Get the options from the builder function. AuthorizaationServiceOptions options = new AuthorizaationServiceOptions(); builder.Invoke(options); // Create an instance of the service and configure. IAuthorizationService instance = Activator.CreateInstance <T>(); instance.Configure(options); // Add the instance to the AuthorizationServiceManager. _authorizationManager.AddAuthorizationService(instance); // Register the configured instance. var serviceDescriptor = services.LastOrDefault(); services.Add(new ServiceDescriptor(serviceDescriptor.ImplementationType, instance)); // Return the IServiceCollection for chainability. return(services); }