net6conversion (#642)
* Converting to net6 startup and integration test formats * Update to minimal api and using pagetitle * Adjust functional test fixtures Update ApiEndpoints package version * Finish migration to minimal api startup
This commit is contained in:
@@ -1,49 +1,198 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.Net.Mime;
|
||||
using Ardalis.ListStartupServices;
|
||||
using BlazorAdmin;
|
||||
using BlazorAdmin.Services;
|
||||
using Blazored.LocalStorage;
|
||||
using BlazorShared;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.Infrastructure.Data;
|
||||
using Microsoft.eShopWeb.Infrastructure.Identity;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.eShopWeb.Web;
|
||||
using Microsoft.eShopWeb.Web.Configuration;
|
||||
using Microsoft.eShopWeb.Web.HealthChecks;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
namespace Microsoft.eShopWeb.Web;
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
builder.Logging.AddConsole();
|
||||
|
||||
// use real database
|
||||
// Requires LocalDB which can be installed with SQL Server Express 2016
|
||||
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
|
||||
builder.Services.AddDbContext<CatalogContext>(c =>
|
||||
c.UseSqlServer(builder.Configuration.GetConnectionString("CatalogConnection")));
|
||||
|
||||
// Add Identity DbContext
|
||||
builder.Services.AddDbContext<AppIdentityDbContext>(options =>
|
||||
options.UseSqlServer(builder.Configuration.GetConnectionString("IdentityConnection")));
|
||||
|
||||
builder.Services.AddCookieSettings();
|
||||
|
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.AddCookie(options =>
|
||||
{
|
||||
var host = CreateHostBuilder(args)
|
||||
.Build();
|
||||
options.Cookie.HttpOnly = true;
|
||||
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
|
||||
options.Cookie.SameSite = SameSiteMode.Lax;
|
||||
});
|
||||
|
||||
using (var scope = host.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
|
||||
try
|
||||
{
|
||||
var catalogContext = services.GetRequiredService<CatalogContext>();
|
||||
await CatalogContextSeed.SeedAsync(catalogContext, loggerFactory);
|
||||
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||
.AddDefaultUI()
|
||||
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
|
||||
await AppIdentityDbContextSeed.SeedAsync(userManager, roleManager);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
logger.LogError(ex, "An error occurred seeding the DB.");
|
||||
}
|
||||
}
|
||||
builder.Services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>();
|
||||
|
||||
host.Run();
|
||||
}
|
||||
builder.Services.AddCoreServices(builder.Configuration);
|
||||
builder.Services.AddWebServices(builder.Configuration);
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
// Add memory cache services
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddRouting(options =>
|
||||
{
|
||||
// Replace the type and the name used to refer to it with your own
|
||||
// IOutboundParameterTransformer implementation
|
||||
options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
|
||||
});
|
||||
|
||||
builder.Services.AddMvc(options =>
|
||||
{
|
||||
options.Conventions.Add(new RouteTokenTransformerConvention(
|
||||
new SlugifyParameterTransformer()));
|
||||
|
||||
});
|
||||
builder.Services.AddControllersWithViews();
|
||||
builder.Services.AddRazorPages(options =>
|
||||
{
|
||||
options.Conventions.AuthorizePage("/Basket/Checkout");
|
||||
});
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services
|
||||
.AddHealthChecks()
|
||||
.AddCheck<ApiHealthCheck>("api_health_check", tags: new[] { "apiHealthCheck" })
|
||||
.AddCheck<HomePageHealthCheck>("home_page_health_check", tags: new[] { "homePageHealthCheck" });
|
||||
builder.Services.Configure<ServiceConfig>(config =>
|
||||
{
|
||||
config.Services = new List<ServiceDescriptor>(builder.Services);
|
||||
config.Path = "/allservices";
|
||||
});
|
||||
|
||||
// blazor configuration
|
||||
var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME);
|
||||
builder.Services.Configure<BaseUrlConfiguration>(configSection);
|
||||
var baseUrlConfig = configSection.Get<BaseUrlConfiguration>();
|
||||
|
||||
// Blazor Admin Required Services for Prerendering
|
||||
builder.Services.AddScoped<HttpClient>(s => new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(baseUrlConfig.WebBase)
|
||||
});
|
||||
|
||||
// add blazor services
|
||||
builder.Services.AddBlazoredLocalStorage();
|
||||
builder.Services.AddServerSideBlazor();
|
||||
|
||||
|
||||
builder.Services.AddScoped<ToastService>();
|
||||
builder.Services.AddScoped<HttpService>();
|
||||
builder.Services.AddBlazorServices();
|
||||
|
||||
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
||||
|
||||
//_services = services; // used to debug registered services
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.Logger.LogInformation("App created...");
|
||||
|
||||
var catalogBaseUrl = builder.Configuration.GetValue(typeof(string), "CatalogBaseUrl") as string;
|
||||
if (!string.IsNullOrEmpty(catalogBaseUrl))
|
||||
{
|
||||
app.Use((context, next) =>
|
||||
{
|
||||
context.Request.PathBase = new PathString(catalogBaseUrl);
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
||||
app.UseHealthChecks("/health",
|
||||
new HealthCheckOptions
|
||||
{
|
||||
ResponseWriter = async (context, report) =>
|
||||
{
|
||||
var result = new
|
||||
{
|
||||
status = report.Status.ToString(),
|
||||
errors = report.Entries.Select(e => new
|
||||
{
|
||||
key = e.Key,
|
||||
value = Enum.GetName(typeof(HealthStatus), e.Value.Status)
|
||||
})
|
||||
}.ToJson();
|
||||
context.Response.ContentType = MediaTypeNames.Application.Json;
|
||||
await context.Response.WriteAsync(result);
|
||||
}
|
||||
});
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.Logger.LogInformation("Adding Development middleware...");
|
||||
app.UseDeveloperExceptionPage();
|
||||
app.UseShowAllServicesMiddleware();
|
||||
app.UseMigrationsEndPoint();
|
||||
app.UseWebAssemblyDebugging();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.Logger.LogInformation("Adding non-Development middleware...");
|
||||
app.UseExceptionHandler("/Error");
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseBlazorFrameworkFiles();
|
||||
app.UseStaticFiles();
|
||||
app.UseRouting();
|
||||
|
||||
app.UseCookiePolicy();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
|
||||
endpoints.MapRazorPages();
|
||||
endpoints.MapHealthChecks("home_page_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("homePageHealthCheck") });
|
||||
endpoints.MapHealthChecks("api_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("apiHealthCheck") });
|
||||
//endpoints.MapBlazorHub("/admin");
|
||||
endpoints.MapFallbackToFile("index.html");
|
||||
});
|
||||
|
||||
app.Logger.LogInformation("Seeding Database...");
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var scopedProvider = scope.ServiceProvider;
|
||||
try
|
||||
{
|
||||
var catalogContext = scopedProvider.GetRequiredService<CatalogContext>();
|
||||
await CatalogContextSeed.SeedAsync(catalogContext, app.Logger);
|
||||
|
||||
var userManager = scopedProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var roleManager = scopedProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||
await AppIdentityDbContextSeed.SeedAsync(userManager, roleManager);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
app.Logger.LogError(ex, "An error occurred seeding the DB.");
|
||||
}
|
||||
}
|
||||
|
||||
app.Logger.LogInformation("LAUNCHING");
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user