コード例 #1
0
        public static TestServer BuildServer <TStartup>(
            IConfigurationRoot configuration = null,
            Action <IServiceCollection> configureServices = null)
            where TStartup : class
        {
            if (configuration == null)
            {
                configuration = ConfigBuilder.BuildConfig();
            }

            string contentRoot = Path.GetFullPath(
                Path.Combine(
                    Directory.GetCurrentDirectory(),
                    "src/IdentityBase.Public"
                    )
                );

            if (!Directory.Exists(contentRoot))
            {
                contentRoot = Path.GetFullPath(
                    Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "../../../../../src/IdentityBase.Public"
                        )
                    );
            }

            HostingEnvironment environment = new HostingEnvironment
            {
                EnvironmentName         = "Development",
                ApplicationName         = "IdentityBase.Public",
                ContentRootPath         = contentRoot,
                ContentRootFileProvider = new PhysicalFileProvider(contentRoot)
            };

            NullLogger <Startup> logger = new NullLogger <Startup>();
            Startup startup             = new Startup(configuration, environment, logger);

            IWebHostBuilder builder = new WebHostBuilder()
                                      .UseContentRoot(contentRoot)
                                      .ConfigureServices(services =>
            {
                configureServices?.Invoke(services);
                services.AddSingleton <IStartup>(startup);
            })
                                      // WORKARROUND: https://github.com/aspnet/Hosting/issues/1137#issuecomment-323234886
                                      .UseSetting(WebHostDefaults.ApplicationKey,
                                                  typeof(Startup).GetTypeInfo().Assembly.FullName);

            return(new TestServer(builder));
        }
コード例 #2
0
        public static TestServer BuildServer(
            Dictionary <string, string> config,
            Action <IServiceCollection> configureServicesAction,
            string environmentName = "Test")
        {
            // Create default configuration if none is passed
            IConfigurationRoot configuration = ConfigBuilder
                                               .Build(config ?? ConfigBuilder.Default);

            var contentRoot = TestServerBuilder2.GetContentRoot();
            var environment = new HostingEnvironment
            {
                EnvironmentName         = environmentName,
                ApplicationName         = "IdentityBase.Public",
                ContentRootPath         = contentRoot,
                ContentRootFileProvider = new PhysicalFileProvider(contentRoot)
            };

            var logger  = new NullLogger <Startup>();
            var startup = new Startup(configuration, environment, logger);

            var builder = new WebHostBuilder()
                          .UseContentRoot(contentRoot)
                          .UseEnvironment(environmentName)
                          .ConfigureServices(services =>
            {
                services.AddSingleton <IStartup>(startup);

                configureServicesAction?.Invoke(services);
            })
                          // WORKARROUND: https://github.com/aspnet/Hosting/issues/1137#issuecomment-323234886
                          .UseSetting(WebHostDefaults.ApplicationKey,
                                      typeof(Startup).GetTypeInfo().Assembly.FullName);

            return(new TestServer(builder));
        }