using ApplicationCore.Entities.OrderAggregate; using ApplicationCore.Interfaces; using ApplicationCore.Services; using Infrastructure.Data; using Infrastructure.Identity; using Infrastructure.Logging; using Infrastructure.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Text; using Web.Services; namespace Microsoft.eShopWeb { public class Startup { private IServiceCollection _services; public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { // Requires LocalDB which can be installed with SQL Server Express 2016 // https://www.microsoft.com/en-us/download/details.aspx?id=54284 services.AddDbContext(c => { try { c.UseInMemoryDatabase("Catalog"); //c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection")); } catch (System.Exception ex ) { var message = ex.Message; } }); // Add Identity DbContext services.AddDbContext(options => options.UseInMemoryDatabase("Identity")); //options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection"))); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.ConfigureApplicationCookie(options => { options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromHours(1); options.LoginPath = "/Account/Signin"; options.LogoutPath = "/Account/Signout"; }); services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>)); services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>)); services.AddMemoryCache(); 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<>)); // Add memory cache services services.AddMemoryCache(); services.AddMvc(); _services = services; } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); ListAllRegisteredServices(app); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Catalog/Error"); } app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(); } private void ListAllRegisteredServices(IApplicationBuilder app) { app.Map("/allservices", builder => builder.Run(async context => { var sb = new StringBuilder(); sb.Append("

All Services

"); sb.Append(""); sb.Append(""); sb.Append(""); foreach (var svc in _services) { sb.Append(""); sb.Append($""); sb.Append($""); sb.Append($""); sb.Append(""); } sb.Append("
TypeLifetimeInstance
{svc.ServiceType.FullName}{svc.Lifetime}{svc.ImplementationType?.FullName}
"); await context.Response.WriteAsync(sb.ToString()); })); } public void ConfigureDevelopment(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, UserManager userManager, CatalogContext catalogContext) { Configure(app, env); //Seed Data CatalogContextSeed.SeedAsync(app, catalogContext, loggerFactory) .Wait(); var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" }; userManager.CreateAsync(defaultUser, "Pass@word1").Wait(); } /// /// Use this section to perform production-specific configuration. /// In this case it is duplicating Development so that deployments to Azure will have sample data immediately. /// /// /// /// /// public void ConfigureProduction(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, UserManager userManager, CatalogContext catalogContext) { Configure(app, env); //Seed Data CatalogContextSeed.SeedAsync(app, catalogContext, loggerFactory) .Wait(); var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" }; userManager.CreateAsync(defaultUser, "Pass@word1").Wait(); } } }