From e5e9868003b2e940c731cdf34a3b8fa2a89d272c Mon Sep 17 00:00:00 2001 From: Eric Fleming Date: Mon, 29 Jun 2020 14:00:54 -0400 Subject: [PATCH] Refactor startup (#412) * Removing unused folder * Refactoring Startup.cs --- .../Configuration/ConfigureCookieSettings.cs | 31 ++++++++++ .../Configuration/ConfigureCoreServices.cs | 24 ++++++++ src/Web/Configuration/ConfigureWebServices.cs | 21 +++++++ src/Web/Startup.cs | 56 ++----------------- src/Web/Web.csproj | 8 ++- 5 files changed, 88 insertions(+), 52 deletions(-) create mode 100644 src/Web/Configuration/ConfigureCookieSettings.cs create mode 100644 src/Web/Configuration/ConfigureCoreServices.cs create mode 100644 src/Web/Configuration/ConfigureWebServices.cs diff --git a/src/Web/Configuration/ConfigureCookieSettings.cs b/src/Web/Configuration/ConfigureCookieSettings.cs new file mode 100644 index 000000000..f80030281 --- /dev/null +++ b/src/Web/Configuration/ConfigureCookieSettings.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Microsoft.eShopWeb.Web.Configuration +{ + public static class ConfigureCookieSettings + { + public static void Configure(IServiceCollection services) + { + services.Configure(options => + { + // This lambda determines whether user consent for non-essential cookies is needed for a given request. + options.CheckConsentNeeded = context => true; + options.MinimumSameSitePolicy = SameSiteMode.None; + }); + services.ConfigureApplicationCookie(options => + { + options.Cookie.HttpOnly = true; + options.ExpireTimeSpan = TimeSpan.FromHours(1); + options.LoginPath = "/Account/Login"; + options.LogoutPath = "/Account/Logout"; + options.Cookie = new CookieBuilder + { + IsEssential = true // required for auth to work without explicit user consent; adjust to suit your privacy policy + }; + }); + } + } +} diff --git a/src/Web/Configuration/ConfigureCoreServices.cs b/src/Web/Configuration/ConfigureCoreServices.cs new file mode 100644 index 000000000..8284297cd --- /dev/null +++ b/src/Web/Configuration/ConfigureCoreServices.cs @@ -0,0 +1,24 @@ +using Microsoft.eShopWeb.ApplicationCore.Interfaces; +using Microsoft.eShopWeb.ApplicationCore.Services; +using Microsoft.eShopWeb.Infrastructure.Data; +using Microsoft.eShopWeb.Infrastructure.Logging; +using Microsoft.eShopWeb.Infrastructure.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.eShopWeb.Web.Configuration +{ + public static class ConfigureCoreServices + { + public static void Configure(IServiceCollection services, IConfiguration configuration) + { + services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>)); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddSingleton(new UriComposer(configuration.Get())); + services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); + services.AddTransient(); + } + } +} diff --git a/src/Web/Configuration/ConfigureWebServices.cs b/src/Web/Configuration/ConfigureWebServices.cs new file mode 100644 index 000000000..92da23702 --- /dev/null +++ b/src/Web/Configuration/ConfigureWebServices.cs @@ -0,0 +1,21 @@ +using MediatR; +using Microsoft.eShopWeb.Web.Interfaces; +using Microsoft.eShopWeb.Web.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.eShopWeb.Web.Configuration +{ + public static class ConfigureWebServices + { + public static void Configure(IServiceCollection services, IConfiguration configuration) + { + services.AddMediatR(typeof(BasketViewModelService).Assembly); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.Configure(configuration); + services.AddScoped(); + } + } +} diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index c6a78fda0..6053871d7 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -1,5 +1,4 @@ using Ardalis.ListStartupServices; -using MediatR; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; @@ -7,25 +6,18 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.EntityFrameworkCore; -using Microsoft.eShopWeb.ApplicationCore.Interfaces; -using Microsoft.eShopWeb.ApplicationCore.Services; using Microsoft.eShopWeb.Infrastructure.Data; using Microsoft.eShopWeb.Infrastructure.Identity; -using Microsoft.eShopWeb.Infrastructure.Logging; -using Microsoft.eShopWeb.Infrastructure.Services; using Microsoft.eShopWeb.Web.API; -using Microsoft.eShopWeb.Web.Interfaces; -using Microsoft.eShopWeb.Web.Services; +using Microsoft.eShopWeb.Web.Configuration; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.Mime; -using static Microsoft.eShopWeb.Web.API.BaseRequest; namespace Microsoft.eShopWeb.Web { @@ -85,38 +77,24 @@ public void ConfigureTestingServices(IServiceCollection services) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - ConfigureCookieSettings(services); + ConfigureCookieSettings.Configure(services); services.AddIdentity() .AddDefaultUI() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); - services.AddMediatR(typeof(BasketViewModelService).Assembly); - - services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>)); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.Configure(Configuration); - services.AddSingleton(new UriComposer(Configuration.Get())); - services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); - services.AddTransient(); + ConfigureCoreServices.Configure(services, Configuration); + ConfigureWebServices.Configure(services, Configuration); // Add memory cache services services.AddMemoryCache(); - services.AddRouting(options => { // Replace the type and the name used to refer to it with your own // IOutboundParameterTransformer implementation options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); }); - services.AddMvc(options => { options.Conventions.Add(new RouteTokenTransformerConvention( @@ -128,18 +106,14 @@ public void ConfigureServices(IServiceCollection services) options.Conventions.AuthorizePage("/Basket/Checkout"); }); services.AddControllersWithViews(); - services.AddHttpContextAccessor(); - services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" }); c.EnableAnnotations(); c.SchemaFilter(); }); - services.AddHealthChecks(); - services.Configure(config => { config.Services = new List(services); @@ -150,26 +124,6 @@ public void ConfigureServices(IServiceCollection services) _services = services; // used to debug registered services } - private static void ConfigureCookieSettings(IServiceCollection services) - { - services.Configure(options => - { - // This lambda determines whether user consent for non-essential cookies is needed for a given request. - options.CheckConsentNeeded = context => true; - options.MinimumSameSitePolicy = SameSiteMode.None; - }); - services.ConfigureApplicationCookie(options => - { - options.Cookie.HttpOnly = true; - options.ExpireTimeSpan = TimeSpan.FromHours(1); - options.LoginPath = "/Account/Login"; - options.LogoutPath = "/Account/Logout"; - options.Cookie = new CookieBuilder - { - IsEssential = true // required for auth to work without explicit user consent; adjust to suit your privacy policy - }; - }); - } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -231,4 +185,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) }); } } -} +} \ No newline at end of file diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 7aa917f90..e4fb3be8f 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -7,6 +7,13 @@ latest + + + + + + + @@ -36,7 +43,6 @@ -