public void StartupWithPrivateConfiguresThrows() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); serviceCollection.AddSingleton <IFakeStartupCallback>(new FakeStartupCallback()); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "PrivateConfigure"); var ex = Assert.Throws <InvalidOperationException>(() => StartupLoader.LoadMethods(services, type, "PrivateConfigure")); Assert.Equal("A public method named 'ConfigurePrivateConfigure' or 'Configure' could not be found in the 'Microsoft.AspNetCore.Hosting.Fakes.StartupPrivateConfigure' type.", ex.Message); }
public void StartupLoaderCanLoadByTypeWithEnvironment() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); var services = serviceCollection.BuildServiceProvider(); var startup = StartupLoader.LoadMethods(services, typeof(TestStartup), "No"); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); Assert.Throws <InvalidOperationException>(() => startup.ConfigureDelegate(app)); }
public void ConfigureThrowingDoesNotThrowTargetInvocationException() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); var services = serviceCollection.BuildServiceProvider(); var startup = StartupLoader.LoadMethods(services, typeof(StartupConfigureThrows), environmentName: null); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); Assert.Throws <Exception>(() => startup.ConfigureDelegate(app)); }
public void BadServiceProviderFactoryFailsThatReturnsNullServiceProviderOverriddenByDefault() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <MyContainer>, MyBadContainerFactory>(); var services = serviceCollection.BuildServiceProvider(); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartup), Environments.Development); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); Assert.NotNull(app.ApplicationServices); Assert.IsNotType <MyContainer>(app.ApplicationServices); }
public DelegatedStatelessWebServiceStartup( IServiceProvider provider, IHostingEnvironment env, Action <IServiceCollection> configureServices) { _configureServices = configureServices; if (typeof(TStartup).IsAssignableTo <IStartup>()) { _startupImplementation = (IStartup)ActivatorUtilities.CreateInstance <TStartup>(provider); } else { StartupMethods methods = StartupLoader.LoadMethods(provider, typeof(TStartup), env.EnvironmentName); _startupImplementation = new ConventionBasedStartup(methods); } }
public void CustomServiceProviderFactoryStartupBaseClassCallsConfigureContainer() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <MyContainer>, MyContainerFactory>(); var services = serviceCollection.BuildServiceProvider(); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupBaseClass), Environments.Development); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); Assert.IsType <MyContainer>(app.ApplicationServices); Assert.True(((MyContainer)app.ApplicationServices).FancyMethodCalled); }
public void CustomServiceProviderFactoryEnvironmentBasedConfigureContainer() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <MyContainer>, MyContainerFactory>(); var services = serviceCollection.BuildServiceProvider(); var startup = StartupLoader.LoadMethods(services, typeof(MyContainerStartupEnvironmentBased), Environments.Production); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); Assert.IsType <MyContainer>(app.ApplicationServices); Assert.Equal(((MyContainer)app.ApplicationServices).Environment, Environments.Production); }
public void StartupClassMayHaveHostingServicesInjected() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IFakeStartupCallback>(this); var services = serviceCollection.BuildServiceProvider(); var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "WithServices"); var startup = StartupLoader.LoadMethods(services, type, "WithServices"); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); startup.ConfigureDelegate(app); Assert.Equal(2, _configurationMethodCalledList.Count); }
public void StartupLoaderCanLoadByType() { var serviceCollection = new ServiceCollection(); var services = serviceCollection.BuildServiceProvider(); var hostingEnv = new HostingEnvironment(); var startup = StartupLoader.LoadMethods(services, typeof(TestStartup), hostingEnv.EnvironmentName); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); startup.ConfigureDelegate(app); var foo = app.ApplicationServices.GetRequiredService <SimpleService>(); Assert.Equal("Configure", foo.Message); }
public void StartupClassWithConfigureServicesShouldMakeServiceAvailableInConfigure() { var serviceCollection = new ServiceCollection(); var services = serviceCollection.BuildServiceProvider(); var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "WithConfigureServices"); var startup = StartupLoader.LoadMethods(services, type, "WithConfigureServices"); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); startup.ConfigureDelegate(app); var foo = app.ApplicationServices.GetRequiredService <StartupWithConfigureServices.IFoo>(); Assert.True(foo.Invoked); }
public void StartupClassCanHandleConfigureServicesThatReturnsNull() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); var services = serviceCollection.BuildServiceProvider(); var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", "WithNullConfigureServices"); var startup = StartupLoader.LoadMethods(services, type, "WithNullConfigureServices"); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection()); Assert.NotNull(app.ApplicationServices); startup.ConfigureDelegate(app); Assert.NotNull(app.ApplicationServices); }
public static IServiceHostBuilder UseStartup(this IServiceHostBuilder hostBuilder, Type startupType) { return(hostBuilder .ConfigureServices(services => { if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton(typeof(IStartup), startupType); } else { services.AddSingleton(typeof(IStartup), sp => { return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, "")); }); } })); }
public void StartupClassAddsConfigureServicesToApplicationServices(string environment) { var services = new ServiceCollection().BuildServiceProvider(); var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", environment); var startup = StartupLoader.LoadMethods(services, type, environment); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection()); startup.ConfigureDelegate(app); var options = app.ApplicationServices.GetRequiredService <IOptions <FakeOptions> >().Value; Assert.NotNull(options); Assert.True(options.Configured); Assert.Equal(environment, options.Environment); }
public void StartupWithTwoConfiguresThrows() { var serviceCollection = new ServiceCollection(); serviceCollection.AddInstance <IFakeStartupCallback>(this); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var hostingEnv = new HostingEnvironment { EnvironmentName = "TwoConfigures" }; var loader = new StartupLoader(services, hostingEnv); var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); var ex = Assert.Throws <InvalidOperationException>(() => loader.LoadMethods(type, diagnosticMessages)); Assert.Equal("Having multiple overloads of method 'Configure' is not supported.", ex.Message); }
public void StartupWithNoConfigureThrows() { var serviceCollection = new ServiceCollection(); serviceCollection.AddInstance <IFakeStartupCallback>(this); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var hostingEnv = new HostingEnvironment { EnvironmentName = "Boom" }; var loader = new StartupLoader(services, hostingEnv); var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); var ex = Assert.Throws <InvalidOperationException>(() => loader.LoadMethods(type, diagnosticMessages)); Assert.Equal("A method named 'ConfigureBoom' or 'Configure' in the type 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' could not be found.", ex.Message); }
public void StartupClassAddsConfigureServicesToApplicationServicesCaseInsensitive(string environment) { var services = new ServiceCollection() .AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>() .BuildServiceProvider(); var type = StartupLoader.FindStartupType("Microsoft.AspNetCore.Hosting.Tests", environment); var startup = StartupLoader.LoadMethods(services, type, environment); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection()); startup.ConfigureDelegate(app); // By this not throwing, it found "ConfigureCaseInsensitive" var options = app.ApplicationServices.GetRequiredService <IOptions <FakeOptions> >().Value; Assert.NotNull(options); Assert.True(options.Configured); Assert.Equal("ConfigureCaseInsensitiveServices", options.Environment); }
public void ConventionalStartupClass_ConfigureContainerFilters_WrapInRegistrationOrder() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <MyContainer>, MyContainerFactory>(); serviceCollection.AddSingleton <IStartupConfigureContainerFilter <MyContainer> >(new TestConfigureContainerFilter(1, overrideAfterService: true)); serviceCollection.AddSingleton <IStartupConfigureContainerFilter <MyContainer> >(new TestConfigureContainerFilter(2, overrideAfterService: true)); var services = serviceCollection.BuildServiceProvider(); var type = typeof(ConfigureContainerStartupServicesFiltersStartup); var startup = StartupLoader.LoadMethods(services, type, ""); var applicationServices = startup.ConfigureServicesDelegate(serviceCollection); var before = applicationServices.GetRequiredService <ServiceBefore>(); var after = applicationServices.GetRequiredService <ServiceAfter>(); Assert.Equal("ConfigureContainerFilter Before 1", before.Message); Assert.Equal("ConfigureContainerFilter After 1", after.Message); }
public void ConventionalStartupClass_StartupServicesFilters_ThrowsIfStartupBuildsTheContainerAsync() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); serviceCollection.AddSingleton <IStartupConfigureServicesFilter>(new TestStartupServicesFilter(1, overrideAfterService: false)); var services = serviceCollection.BuildServiceProvider(); var type = typeof(IServiceProviderReturningStartupServicesFiltersStartup); var startup = StartupLoader.LoadMethods(services, type, ""); var expectedMessage = $"A ConfigureServices method that returns an {nameof(IServiceProvider)} is " + $"not compatible with the use of one or more {nameof(IStartupConfigureServicesFilter)}. " + $"Use a void returning ConfigureServices method instead or a ConfigureContainer method."; var exception = Assert.Throws <InvalidOperationException>(() => startup.ConfigureServicesDelegate(serviceCollection)); Assert.Equal(expectedMessage, exception.Message); }
public void ConventionalStartupClass_StartupServiceFilters_MultipleStartupServiceFiltersRun() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); serviceCollection.AddSingleton <IStartupConfigureServicesFilter>(new TestStartupServicesFilter(1, overrideAfterService: false)); serviceCollection.AddSingleton <IStartupConfigureServicesFilter>(new TestStartupServicesFilter(2, overrideAfterService: true)); var services = serviceCollection.BuildServiceProvider(); var type = typeof(VoidReturningStartupServicesFiltersStartup); var startup = StartupLoader.LoadMethods(services, type, ""); var applicationServices = startup.ConfigureServicesDelegate(serviceCollection); var before = applicationServices.GetRequiredService <ServiceBefore>(); var after = applicationServices.GetRequiredService <ServiceAfter>(); Assert.Equal("StartupServicesFilter Before 1", before.Message); Assert.Equal("StartupServicesFilter After 2", after.Message); }
public static IWorkerHostBuilder UseStartup(this IWorkerHostBuilder hostBuilder, Type startupType) { var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name; if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { hostBuilder.Services.AddSingleton(typeof(IStartup), startupType); } else { hostBuilder.Services.AddSingleton(typeof(IStartup), sp => { var hostingEnvironment = sp.GetRequiredService <IWorkerHostingEnvironment>(); return(new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName))); }); } return(hostBuilder); }
public static IGrpcHostBuilder UseStartup(this IGrpcHostBuilder builder, Type startupType) { return(builder.ConfigureServices(services => { if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton(typeof(IStartup), startupType); } else { services.AddSingleton(typeof(IStartup), sp => { var hostingEnvironment = sp.GetRequiredService <IHostingEnvironment>(); return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName)); }); } })); }
private void InitServerAndClient() { Server = new TestServer(WebHost.CreateDefaultBuilder() .ConfigureServices((context, services) => services.AddSingleton((IConfigurationRoot)context.Configuration)) .ConfigureServices(services => { services.AddSingleton <IStartup>(sp => { var startupMethods = StartupLoader.LoadMethods(sp, typeof(TStartup), Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); return(new TestHostStartupWrapper(this, startupMethods)); }); }) // Todo: remove once fixed (see https://github.com/aspnet/Hosting/issues/1137) .UseSetting(WebHostDefaults.ApplicationKey, typeof(TStartup).Assembly.FullName) ); Client = Server.CreateClient(); }
public DelegatedStatelessWebServiceStartup( IServiceProvider provider, IHostEnvironment env, Action<IServiceCollection> configureServices) { _configureServices = configureServices; if (typeof(IStartup).IsAssignableFrom(typeof(TStartup))) { _startupImplementation = (IStartup) ActivatorUtilities.CreateInstance<TStartup>(provider)!; } else { #if !NETCOREAPP3_1 var methods = StartupLoader.LoadMethods(provider, typeof(TStartup), env.EnvironmentName); _startupImplementation = new ConventionBasedStartup(methods); #else throw new InvalidOperationException($"Type '{typeof(TStartup).FullName}' must implement {typeof(IStartup).FullName}"); #endif } }
public void StartupClassCanHandleConfigureServicesThatReturnsNull() { var serviceCollection = new ServiceCollection(); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var hostingEnv = new HostingEnvironment { EnvironmentName = "WithNullConfigureServices" }; var loader = new StartupLoader(services, hostingEnv); var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); var startup = loader.LoadMethods(type, diagnosticMessages); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(new ServiceCollection()); Assert.NotNull(app.ApplicationServices); startup.ConfigureDelegate(app); Assert.NotNull(app.ApplicationServices); }
public void StartupLoaderCanLoadByTypeWithEnvironment() { var serviceCollection = new ServiceCollection(); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var hostingEnv = new HostingEnvironment { EnvironmentName = "No" }; var loader = new StartupLoader(services, hostingEnv); var startup = loader.LoadMethods(typeof(TestStartup), diagnosticMessages); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); var ex = Assert.Throws <TargetInvocationException>(() => startup.ConfigureDelegate(app)); Assert.IsAssignableFrom(typeof(InvalidOperationException), ex.InnerException); }
/// <summary> /// Configures the host services with a Startup class /// </summary> /// <typeparam name ="TStartup">The type used to configure services</typeparam> /// <param name="hostBuilder">The <see cref="IXamarinHostBuilder"/> to configure.</param> /// <param name="startup">The instance of startup class to configure services with</param> /// <returns>The <see cref="IXamarinHostBuilder"/>.</returns> public static IXamarinHostBuilder UseStartup <TStartup>(this IXamarinHostBuilder hostBuilder, TStartup startup) where TStartup : class { var startupType = typeof(TStartup); return(hostBuilder .ConfigureServices((context, services) => { if (typeof(IXamarinStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton((IXamarinStartup)startup); } else { services.AddSingleton <IXamarinStartup>(sp => { var hostingEnvironment = sp.GetRequiredService <IXamarinHostEnvironment>(); return new XamarinStartup(StartupLoader.LoadMethods(startup, hostingEnvironment.EnvironmentName)); }); } })); }
public void ConventionalStartupClass_StartupServiceFilters_WrapsConfigureServicesMethod() { var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IServiceProviderFactory <IServiceCollection>, DefaultServiceProviderFactory>(); #pragma warning disable CS0612 // Type or member is obsolete serviceCollection.AddSingleton <IStartupConfigureServicesFilter>(new TestStartupServicesFilter(1, overrideAfterService: true)); serviceCollection.AddSingleton <IStartupConfigureServicesFilter>(new TestStartupServicesFilter(2, overrideAfterService: true)); #pragma warning restore CS0612 // Type or member is obsolete var services = serviceCollection.BuildServiceProvider(); var type = typeof(VoidReturningStartupServicesFiltersStartup); var startup = StartupLoader.LoadMethods(services, type, ""); var applicationServices = startup.ConfigureServicesDelegate(serviceCollection); var before = applicationServices.GetRequiredService <ServiceBefore>(); var after = applicationServices.GetRequiredService <ServiceAfter>(); Assert.Equal("StartupServicesFilter Before 1", before.Message); Assert.Equal("StartupServicesFilter After 1", after.Message); }
public InternalVirtualHost( PathString appBase, List <IHostModule> modules) { _appBase = appBase; Pages = new ReadOnlyCollection <Page>(modules.SelectMany(x => x.Pages).ToList()); _testServer = new TestServer(new WebHostBuilder() .UseSetting(WebHostDefaults.ApplicationKey, Assembly.GetEntryAssembly().GetName().Name) .ConfigureLogging(factory => { factory.AddConsole(); }) .ConfigureServices(services => { services.AddSingleton <Startup>(); services.AddSingleton(modules); services.AddSingleton(typeof(IStartup), sp => { var hostingEnvironment = sp.GetRequiredService <IHostingEnvironment>(); return(new ConventionBasedStartup(StartupLoader.LoadMethods(sp, typeof(Startup), hostingEnvironment.EnvironmentName))); }); })); }
public void StartupClassMayHaveHostingServicesInjected() { var serviceCollection = new ServiceCollection(); serviceCollection.AddInstance <IFakeStartupCallback>(this); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var hostingEnv = new HostingEnvironment { EnvironmentName = "WithServices" }; var loader = new StartupLoader(services, hostingEnv); var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); var startup = loader.LoadMethods(type, diagnosticMessages); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); startup.ConfigureDelegate(app); Assert.Equal(2, _configurationMethodCalledList.Count); }
public void StartupClassWithConfigureServicesShouldMakeServiceAvailableInConfigure() { var serviceCollection = new ServiceCollection(); var services = serviceCollection.BuildServiceProvider(); var diagnosticMessages = new List <string>(); var hostingEnv = new HostingEnvironment { EnvironmentName = "WithConfigureServices" }; var loader = new StartupLoader(services, hostingEnv); var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); var startup = loader.LoadMethods(type, diagnosticMessages); var app = new ApplicationBuilder(services); app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection); startup.ConfigureDelegate(app); var foo = app.ApplicationServices.GetRequiredService <StartupWithConfigureServices.IFoo>(); Assert.True(foo.Invoked); }