Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // set up our EF data context
            var connection = Configuration.GetConnectionString(ConnectionStrings.SqlServer);

            services.AddDbContext <EmailServiceContext>(options =>
            {
                options.UseMemoryCache(null);
                options.UseSqlServer(connection, sqlOptions =>
                {
                    sqlOptions.MigrationsAssembly("EmailService.Web");
                    sqlOptions.EnableRetryOnFailure();
                });
            });

            // add custom services
            services.AddTransient <IApplicationKeyStore, DbApplicationKeyStore>();
            services.AddSingleton <ICryptoServices>(RsaCryptoServices.Instance);
            services.AddSingleton <IEmailTransportFactory>(EmailTransportFactory.Instance);
            services.AddAzureStorageServices(options =>
            {
                options.ConnectionString = Configuration.GetConnectionString(ConnectionStrings.Storage);
            });

            services.Configure <KestrelServerOptions>(options =>
            {
                if (HostingEnv.IsDevelopment())
                {
                    options.UseHttps("localhost.pfx", "0dinpa55");
                }
            });

            // set up AI telemetry
            services.AddApplicationInsightsTelemetry(Configuration);

            // Add framework services.
            services.AddMvc(options =>
            {
                if (HostingEnv.IsDevelopment())
                {
                    options.SslPort = 44320;
                }

                options.Filters.Add(new RequireHttpsAttribute());
            });

            services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            });
        }
Example #2
0
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            if (HostingEnv.IsDevelopment())
            {
                loggerFactory.AddConsole(Startup.Configuration.GetSection("Logging"));
                loggerFactory.AddDebug();
            }
            else
            {
                loggerFactory.AddMailServiceDatabase(options => {
                    options.ConnectionString   = Configuration.GetConnectionString("ErrorLogMailServiceConnectionString");
                    options.CurrentHttpContext = () => app.ApplicationServices.GetRequiredService <IHttpContextAccessor>();
                });
            }

            if (this._startupExceptions.Any(ex => ex.Value.Count > 0))
            {
                var logger = loggerFactory.CreateLogger(ApplicationName);

                app.Run(async context => {
                    context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    context.Response.ContentType = "text/plain";

                    foreach (var ex in this._startupExceptions)
                    {
                        foreach (var val in ex.Value)
                        {
                            logger.LogError($"{ex.Key}:::{val.Message}");
                            await context.Response.WriteAsync($"Error on {ex.Key}: {val.Message}").ConfigureAwait(false);
                        }
                    }
                });
                return;
            }

            //app.UseCustomisedCsp();

            //app.UseCustomisedHeadersMiddleware();

            app.AddDevMiddlewares();

            app.UseStaticFiles();

            if (HostingEnv.IsProduction())
            {
                app.UseResponseCompression();
            }

            app.AddCustomLocalization();
            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                // http://stackoverflow.com/questions/25982095/using-googleoauth2authenticationoptions-got-a-redirect-uri-mismatch-error
                // routes.MapRoute(name: "signin-google", template: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });

                // Route for: /setLangugage/?culture=en-GB
                routes.MapRoute(name: "set-language", template: "setlanguage", defaults: new { controller = "Home", action = "SetLanguage" });

                routes.MapSpaFallbackRoute(name: "spa-fallback", defaults: new { controller = "Home", action = "Index" });
            });
        }