예제 #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
        void Dispose(bool disposing)
        {
            if (disposing)
            {
                GC.SuppressFinalize(this);
            }


            if (_dataContext != null)
            {
                (_dataContext as IDisposable).Dispose();
                _dataContext = null;
            }

            if (_applicationContext != null)
            {
                (_applicationContext as IDisposable).Dispose();
                _applicationContext = null;
            }
        }