// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Matchers; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection { public static class DispatcherServiceCollectionExtensions { public static IServiceCollection AddDispatcher(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } // Collect all data sources from DI. services.TryAddEnumerable(ServiceDescriptor.Transient, ConfigureDispatcherOptions>()); // Allow global access to the list of endpoints. services.TryAddSingleton(s => { var options = s.GetRequiredService>(); return new CompositeEndpointDataSource(options.Value.DataSources); }); // // Default matcher implementation // services.TryAddSingleton(); return services; } public static IServiceCollection AddDispatcher(this IServiceCollection services, Action configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddDispatcher(); if (configuration != null) { services.Configure(configuration); } return services; } } }