Adding Razor Pages Version (#60)
* In progress copying code into new RP project Cleaning up namespaces and whitespace in original Web project * Cleaning up some more namespaces * Removing unused page. * Index page loads correctly. * Fixing up paging. * Moving views; getting ready to convert to RPs * Auto stash before merge of "master" and "origin/master" Basket and Checkout pages wired up * WIP on Account pages * Working on signin/signout * Working on auth * Getting order history working Fixing auth bug * Fixing Checkout issue * Fixing link
This commit is contained in:
156
src/WebRazorPages/Startup.cs
Normal file
156
src/WebRazorPages/Startup.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
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.RazorPages.Interfaces;
|
||||
using Microsoft.eShopWeb.RazorPages.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
private IServiceCollection _services;
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public void ConfigureDevelopmentServices(IServiceCollection services)
|
||||
{
|
||||
// use in-memory database
|
||||
ConfigureTestingServices(services);
|
||||
|
||||
// use real database
|
||||
// ConfigureProductionServices(services);
|
||||
|
||||
}
|
||||
public void ConfigureTestingServices(IServiceCollection services)
|
||||
{
|
||||
// use in-memory database
|
||||
services.AddDbContext<CatalogContext>(c =>
|
||||
c.UseInMemoryDatabase("Catalog"));
|
||||
|
||||
// Add Identity DbContext
|
||||
services.AddDbContext<AppIdentityDbContext>(options =>
|
||||
options.UseInMemoryDatabase("Identity"));
|
||||
|
||||
ConfigureServices(services);
|
||||
}
|
||||
|
||||
public void ConfigureProductionServices(IServiceCollection services)
|
||||
{
|
||||
// use real database
|
||||
services.AddDbContext<CatalogContext>(c =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// Requires LocalDB which can be installed with SQL Server Express 2016
|
||||
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
|
||||
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
var message = ex.Message;
|
||||
}
|
||||
});
|
||||
|
||||
// Add Identity DbContext
|
||||
services.AddDbContext<AppIdentityDbContext>(options =>
|
||||
options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
|
||||
|
||||
ConfigureServices(services);
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
||||
.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.AddScoped<ICatalogService, CachedCatalogService>();
|
||||
services.AddScoped<IBasketService, BasketService>();
|
||||
services.AddScoped<IOrderService, OrderService>();
|
||||
services.AddScoped<IOrderRepository, OrderRepository>();
|
||||
services.AddScoped<CatalogService>();
|
||||
services.Configure<CatalogSettings>(Configuration);
|
||||
services.AddSingleton<IUriComposer>(new UriComposer(Configuration.Get<CatalogSettings>()));
|
||||
|
||||
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("<h1>All Services</h1>");
|
||||
sb.Append("<table><thead>");
|
||||
sb.Append("<tr><th>Type</th><th>Lifetime</th><th>Instance</th></tr>");
|
||||
sb.Append("</thead><tbody>");
|
||||
foreach (var svc in _services)
|
||||
{
|
||||
sb.Append("<tr>");
|
||||
sb.Append($"<td>{svc.ServiceType.FullName}</td>");
|
||||
sb.Append($"<td>{svc.Lifetime}</td>");
|
||||
sb.Append($"<td>{svc.ImplementationType?.FullName}</td>");
|
||||
sb.Append("</tr>");
|
||||
}
|
||||
sb.Append("</tbody></table>");
|
||||
await context.Response.WriteAsync(sb.ToString());
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user