/// <summary> /// Adds Carter to the specified <see cref="IServiceCollection"/>. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/> to add Carter to.</param> public static void AddCarter(this IServiceCollection services) { var assemblyCatalog = new DependencyContextAssemblyCatalog(); var assemblies = assemblyCatalog.GetAssemblies(); CarterDiagnostics diagnostics = new CarterDiagnostics(); services.AddSingleton(diagnostics); var validators = assemblies.SelectMany(ass => ass.GetTypes()) .Where(typeof(IValidator).IsAssignableFrom) .Where(t => !t.GetTypeInfo().IsAbstract); foreach (var validator in validators) { diagnostics.AddValidator(validator); services.AddSingleton(typeof(IValidator), validator); } services.AddSingleton <IValidatorLocator, DefaultValidatorLocator>(); services.AddRouting(); var modules = assemblies.SelectMany(x => x.GetTypes() .Where(t => !t.IsAbstract && typeof(CarterModule).IsAssignableFrom(t) && t != typeof(CarterModule) && t.IsPublic )); foreach (var module in modules) { diagnostics.AddModule(module); services.AddScoped(module); services.AddScoped(typeof(CarterModule), module); } var schs = assemblies.SelectMany(x => x.GetTypes().Where(t => typeof(IStatusCodeHandler).IsAssignableFrom(t) && t != typeof(IStatusCodeHandler))); foreach (var sch in schs) { diagnostics.AddStatusCodeHandler(sch); services.AddScoped(typeof(IStatusCodeHandler), sch); } var responseNegotiators = assemblies.SelectMany(x => x.GetTypes().Where(t => typeof(IResponseNegotiator).IsAssignableFrom(t) && t != typeof(IResponseNegotiator))); foreach (var negotiatator in responseNegotiators) { diagnostics.AddResponseNegotiator(negotiatator); services.AddSingleton(typeof(IResponseNegotiator), negotiatator); } services.AddSingleton <IResponseNegotiator, DefaultJsonResponseNegotiator>(); }
/// <summary> /// Adds Carter to the specified <see cref="IServiceCollection"/>. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/> to add Carter to.</param> /// <param name="assemblyCatalog">Optional <see cref="DependencyContextAssemblyCatalog"/> containing assemblies to add to the services collection. If not provided, the default catalog of assemblies is added, which includes Assembly.GetEntryAssembly.</param> /// <param name="configurator">Optional <see cref="CarterConfigurator"/> to enable registration of specific types within Carter</param> public static void AddCarter(this IServiceCollection services, DependencyContextAssemblyCatalog assemblyCatalog = null, Action <CarterConfigurator> configurator = null) { assemblyCatalog = assemblyCatalog ?? new DependencyContextAssemblyCatalog(); var config = new CarterConfigurator(); configurator?.Invoke(config); WireupCarter(services, assemblyCatalog, config); }
private static void WireupCarter(this IServiceCollection services, DependencyContextAssemblyCatalog assemblyCatalog, CarterConfigurator carterConfigurator, Action <CarterOptions> options) { var assemblies = assemblyCatalog.GetAssemblies(); var validators = GetValidators(carterConfigurator, assemblies); var modules = GetModules(carterConfigurator, assemblies); var statusCodeHandlers = GetStatusCodeHandlers(carterConfigurator, assemblies); var responseNegotiators = GetResponseNegotiators(carterConfigurator, assemblies); var modelBinder = GetModelBinder(carterConfigurator, assemblies); services.Configure(options); services.AddSingleton(carterConfigurator); foreach (var validator in validators) { services.AddSingleton(typeof(IValidator), validator); } services.AddSingleton <IValidatorLocator, DefaultValidatorLocator>(); services.AddRouting(); foreach (var module in modules) { services.AddScoped(module); services.AddScoped(typeof(CarterModule), module); } foreach (var sch in statusCodeHandlers) { services.AddScoped(typeof(IStatusCodeHandler), sch); } foreach (var negotiator in responseNegotiators) { services.AddSingleton(typeof(IResponseNegotiator), negotiator); } if (modelBinder != null) { services.AddSingleton(typeof(IModelBinder), modelBinder); } else { services.AddSingleton <IModelBinder, DefaultJsonModelBinder>(); } services.AddSingleton <IResponseNegotiator, DefaultJsonResponseNegotiator>(); }