예제 #1
0
        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);
        }
예제 #2
0
        public void Configure(IApplicationBuilder app)
        {
            try
            {
                _methods.ConfigureDelegate(app);
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                }

                throw;
            }
        }
예제 #3
0
 public void Configure(IApplicationBuilder app)
 {
     _testHost.BeforeConfigure(app);
     _startupMethods.ConfigureDelegate(app);
     _testHost.AfterConfigure(app);
 }