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 public method named 'ConfigureBoom' or 'Configure' could not be found in the 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' type.", ex.Message);
        }
        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 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 StartupClassAddsConfigureServicesToApplicationServices(string environment)
        {
            var services = new ServiceCollection().BuildServiceProvider();

            var diagnosticMessages = new List<string>();
            var hostingEnv = new HostingEnvironment { EnvironmentName = environment };
            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());
            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 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);
        }
        public void StartupLoaderCanLoadByType()
        {
            var serviceCollection = new ServiceCollection();
            var services = serviceCollection.BuildServiceProvider();

            var diagnosticMessages = new List<string>();
            var hostingEnv = new HostingEnvironment();
            var loader = new StartupLoader(services, hostingEnv);
            var startup = loader.LoadMethods(typeof(TestStartup), diagnosticMessages);

            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 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);
        }