public IServiceProvider ConfigureServices(IServiceCollection services) { _testHost._services = services; _testHost.BeforeConfigureServices(services); _startupMethods.ConfigureServicesDelegate(services); _testHost.AfterConfigureServices(services); return(_testHost._provider = services.BuildServiceProvider()); }
public static TestApplicationContext BuildApplication(Action <IConfigurationBuilder> customConfiguration, Action <IServiceCollection> serviceConfiguration, Action <IApplicationBuilder, IHostingEnvironment, ILoggerFactory> serviceCustomConfiguration, string environmentName = "Production") { StartupMethods startup = null; IApplicationBuilder bootingupApp = null; var bootstrapDiagnosticMessages = new List <string>(); var testApp = new TestApplicationContext { LoggerFactory = new StubLoggerFactory(), ApplicationEnvironment = CreateApplicationEnvironment(), HostingEnvironment = new HostingEnvironment { EnvironmentName = environmentName } }; testApp.Configuration = BuildConfiguration(testApp.HostingEnvironment, testApp.ApplicationEnvironment, customConfiguration); Func <IServiceCollection, IServiceProvider> configureServices = services => { services.AddInstance <ILoggerFactory>(testApp.LoggerFactory); services.AddInstance <IApplicationEnvironment>(testApp.ApplicationEnvironment); var loader = new StartupLoader(services.BuildServiceProvider(), testApp.HostingEnvironment); startup = loader.LoadMethods(typeof(Startup), bootstrapDiagnosticMessages); startup.ConfigureServicesDelegate(services); serviceConfiguration(services); testApp.ApplicationServices = services.BuildServiceProvider(); return(testApp.ApplicationServices); }; Action <IApplicationBuilder> configure = app => { bootingupApp = app; startup.ConfigureDelegate(app); serviceCustomConfiguration(app, testApp.HostingEnvironment, testApp.ApplicationServices.GetService <ILoggerFactory>()); }; var webHostBuilder = TestServer .CreateBuilder(testApp.Configuration) .UseEnvironment(environmentName) .UseStartup(configure, configureServices); testApp.Server = new TestServer(webHostBuilder); testApp.RequestHandler = bootingupApp.Build(); return(testApp); }
private static void PrepareServices(IServiceCollection serviceCollection, StartupMethods startupMethods) { if (startupMethods?.ConfigureServicesDelegate != null) { startupMethods.ConfigureServicesDelegate(serviceCollection); } else { var defaultRegistrationPlugin = DefaultRegistrationPlugins .OrderByDescending(p => p.Priority) .FirstOrDefault(); if (defaultRegistrationPlugin != null) { defaultRegistrationPlugin.DefaultServiceRegistrationDelegate(serviceCollection); } else { serviceCollection .AddMvcCore() .AddFormatterMappings() .AddJsonFormatters(); } } AdditionalServices?.Invoke(serviceCollection); // custom MVC options serviceCollection.Configure <MvcOptions>(options => { // add controller conventions to save all valid controller types options.Conventions.Add(new ValidControllersCache()); // string input formatter helps with HTTP request processing var inputFormatters = options.InputFormatters.OfType <TextInputFormatter>(); if (!inputFormatters.Any(f => f.SupportedMediaTypes.Contains(ContentType.TextPlain))) { options.InputFormatters.Add(new StringInputFormatter()); } }); TryReplaceKnownServices(serviceCollection); PrepareRouteServices(serviceCollection); serviceProvider = serviceCollection.BuildServiceProvider(); // this call prepares all application conventions and fills the controller action descriptor cache serviceProvider.GetService <IControllerActionDescriptorCache>(); }
private static void PrepareServices(IServiceCollection serviceCollection, StartupMethods startupMethods) { if (startupMethods?.ConfigureServicesDelegate != null) { startupMethods.ConfigureServicesDelegate(serviceCollection); } else { var defaultRegistrationPlugin = DefaultRegistrationPlugins .OrderByDescending(p => p.Priority) .FirstOrDefault(); if (defaultRegistrationPlugin != null) { defaultRegistrationPlugin.DefaultServiceRegistrationDelegate(serviceCollection); } else { serviceCollection.AddMvcCore(); } } AdditionalServices?.Invoke(serviceCollection); TryReplaceKnownServices(serviceCollection); PrepareRoutingServices(serviceCollection); #if NET451 var baseStartupType = StartupType; while (baseStartupType != null && baseStartupType?.BaseType != typeof(object)) { baseStartupType = baseStartupType.BaseType; } var applicationPartManager = (ApplicationPartManager)serviceCollection .FirstOrDefault(t => t.ServiceType == typeof(ApplicationPartManager)) ?.ImplementationInstance; if (applicationPartManager != null && baseStartupType != null) { applicationPartManager.ApplicationParts.Add(new AssemblyPart(baseStartupType.GetTypeInfo().Assembly)); } #endif serviceProvider = serviceCollection.BuildServiceProvider(); InitializationPlugins.ForEach(plugin => plugin.InitializationDelegate(serviceProvider)); }
public IServiceProvider ConfigureServices(IServiceCollection services) { try { return(_methods.ConfigureServicesDelegate(services)); } catch (Exception ex) { if (ex is TargetInvocationException) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } throw; } }