Beispiel #1
0
        public CompositionTests()
        {
            // Load real configuration
            _configuration = new ConfigurationBuilder()
                             .SetBasePath(Path.GetDirectoryName(typeof(Program).Assembly.Location))
                             .AddJsonFile("appsettings.json", false, true)
                             .AddJsonFile("appsettings.Development.json", false, true)
                             .Build();

            // Build services
            var serviceCollection    = new ServiceCollection();
            var bootstrapperSettings = new BootstrapperSettings {
                EntryAssembly     = typeof(Program).Assembly,
                EnvironmentName   = "",
                UseDetailedErrors = true
            };
            var hostingEnvironment = new FakeHostingEnvironment();

            Composition.ConfigureServices(serviceCollection, hostingEnvironment, _configuration, bootstrapperSettings);

            // Add registrations that are performed by the WebHostStartup
            serviceCollection
            .AddSingleton <IHostingEnvironment>(hostingEnvironment)
            .AddSingleton(bootstrapperSettings)
            .AddMvc()
            .AddApplicationPart(typeof(DefaultController).Assembly)
            .AddControllersAsServices()
            .AddApplicationPart(typeof(Controllers.DefaultController).Assembly)
            .AddControllersAsServices();

            // Build service provider
            _serviceProvider = serviceCollection.BuildServiceProvider();
        }
        public static void ConfigureServices(
            IServiceCollection services,
            IHostingEnvironment hostingEnv,
            IConfiguration configuration,
            BootstrapperSettings bootstrapperSettings)
        {
            // Configuration
            var authSettings    = services.ConfigureSettings <AuthenticationSettings>(configuration.GetSection("Authentication"));
            var networkSettings = services.ConfigureSettings <NetworkSettings>(configuration.GetSection("Network"));

            // Features and services
            services
            .AddFileProviders()
            .AddAzureAdAuthentication(authSettings)
            .AddSwagger(bootstrapperSettings, authSettings)
            .AddSerilog(configuration)
            .AddPreconfiguredJsonSerializer()
            .AddApplicationInfo()
            .AddAllLinksCreators()
            .ConfigureCookiePolicy();

            if (!bootstrapperSettings.DisableHttpsRedirection)
            {
                services.AddHttpsRedirection(options => {
                    options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
                    options.HttpsPort          = networkSettings.HttpsPort;
                });
            }

            // Development configuration
            if (hostingEnv.IsDevelopmentOrDebug())
            {
                IdentityModelEventSource.ShowPII = true;
            }
        }
 public static IConfigurationRoot BuildConfiguration(BootstrapperSettings settings, string[] args)
 {
     return(new ConfigurationBuilder()
            .SetBasePath(Path.GetDirectoryName(settings.EntryAssembly.Location))
            .AddJsonFile("appsettings.json", false, true)
            .AddJsonFile($"appsettings.{settings.EnvironmentName}.json", true, true)
            .AddCommandLine(args)
            .AddEnvironmentVariables()
            .Build());
 }
Beispiel #4
0
        public static int RunDevelopment(string[] args)
        {
            if (!bool.TryParse(Environment.GetEnvironmentVariable("DISABLE_HTTPSREDIRECTION"), out var disableHttpRedirection))
            {
                disableHttpRedirection = false;
            }
            var bootstrapperSettings = new BootstrapperSettings {
                EnvironmentName         = EnvironmentName.Development,
                EntryAssembly           = EntryAssembly,
                UseDetailedErrors       = true,
                DisableHttpsRedirection = disableHttpRedirection
            };
            var configuration = Configuration.BuildConfiguration(bootstrapperSettings, args);
            var webHost       = BuildWebHost(configuration, bootstrapperSettings);

            webHost.Run();
            return(0);
        }
Beispiel #5
0
        private static int RunProduction(string[] args)
        {
            if (!bool.TryParse(Environment.GetEnvironmentVariable("DISABLE_HTTPSREDIRECTION"), out var disableHttpRedirection))
            {
                disableHttpRedirection = false;
            }
            var bootstrapperSettings = new BootstrapperSettings {
                EnvironmentName         = EnvironmentName.Production,
                EntryAssembly           = EntryAssembly,
                UseDetailedErrors       = false,
                DisableHttpsRedirection = disableHttpRedirection
            };
            var configuration = Configuration.BuildConfiguration(bootstrapperSettings, args);

            try {
                var webHost = BuildWebHost(configuration, bootstrapperSettings);
                webHost.Run();
                return(0);
            }
            catch (Exception) {
                return(1);
            }
        }
Beispiel #6
0
        public static IServiceCollection AddSwagger(this IServiceCollection services, BootstrapperSettings bootstrapperSettings, AuthenticationSettings authSettings)
        {
            return(services
                   .AddTransient <IConfigureOptions <SwaggerGenOptions>, ConfigureSwaggerOptions>()
                   .AddSwaggerGen(options => {
                var pathToEntryAssembly = bootstrapperSettings.EntryAssembly.Location;
                var pathToContentRoot = Path.GetDirectoryName(pathToEntryAssembly);
                var assemblyName = Path.GetFileNameWithoutExtension(pathToEntryAssembly);
                options.IncludeXmlComments(Path.Combine(pathToContentRoot, assemblyName + ".xml"));
                var apiAssemblyName = Path.GetFileNameWithoutExtension(typeof(Api.Controllers.DefaultController).Assembly.Location);
                var xmlFile = Path.Combine(pathToContentRoot, apiAssemblyName + ".xml");
                options.IncludeXmlComments(xmlFile);
                options.IncludeControllerDisplayNameAndDescription(xmlFile);
                options.DescribeAllEnumsAsStrings();
                options.DescribeStringEnumsInCamelCase();

                var authority = authSettings.SignInEndpoint.WithRelativePath(authSettings.Tenant);
                options.AddSecurityDefinition("oauth2", new OAuth2Scheme {
                    Flow = "implicit",
                    AuthorizationUrl = $"{authority}/oauth2/authorize"
                });
                options.OperationFilter <AuthorizeCheckOperationFilter>();
                options.OperationFilter <SwaggerDefaultValues>();
            }));
        }
Beispiel #7
0
 internal static IWebHost BuildWebHost(IConfigurationRoot configuration, BootstrapperSettings settings)
 {
     return(CreateWebHostBuilder(configuration, settings).Build());
 }
Beispiel #8
0
 internal static IWebHostBuilder CreateWebHostBuilder(IConfigurationRoot configuration, BootstrapperSettings settings)
 {
     return(new WebHostBuilder()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseKestrel()
            .UseIIS()
            .UseIISIntegration()
            .CaptureStartupErrors(true)
            .UseSetting(WebHostDefaults.DetailedErrorsKey, settings.UseDetailedErrors.ToString())
            .UseConfiguration(configuration)
            .ConfigureAppConfiguration((context, config) => context.HostingEnvironment.EnvironmentName = settings.EnvironmentName)
            .ConfigureServices((context, services) => {
         services.AddSingleton(context.Configuration);
         services.AddSingleton(settings);
     })
            .UseStartup <WebHostStartup>()
            .ConfigureServices((context, services) => Composition.ConfigureServices(services, context.HostingEnvironment, context.Configuration, settings))
            .UseSerilog());
 }