|
1 | 1 | namespace AspNetCoreTemplate.Web
|
2 | 2 | {
|
3 |
| - using Microsoft.AspNetCore.Hosting; |
| 3 | + using System.Reflection; |
| 4 | + |
| 5 | + using AspNetCoreTemplate.Data; |
| 6 | + using AspNetCoreTemplate.Data.Common; |
| 7 | + using AspNetCoreTemplate.Data.Common.Repositories; |
| 8 | + using AspNetCoreTemplate.Data.Models; |
| 9 | + using AspNetCoreTemplate.Data.Repositories; |
| 10 | + using AspNetCoreTemplate.Data.Seeding; |
| 11 | + using AspNetCoreTemplate.Services.Data; |
| 12 | + using AspNetCoreTemplate.Services.Mapping; |
| 13 | + using AspNetCoreTemplate.Services.Messaging; |
| 14 | + using AspNetCoreTemplate.Web.ViewModels; |
| 15 | + |
| 16 | + using Microsoft.AspNetCore.Builder; |
| 17 | + using Microsoft.AspNetCore.Http; |
| 18 | + using Microsoft.AspNetCore.Mvc; |
| 19 | + using Microsoft.EntityFrameworkCore; |
| 20 | + using Microsoft.Extensions.Configuration; |
| 21 | + using Microsoft.Extensions.DependencyInjection; |
4 | 22 | using Microsoft.Extensions.Hosting;
|
5 | 23 |
|
6 |
| - public static class Program |
| 24 | + public class Program |
7 | 25 | {
|
8 | 26 | public static void Main(string[] args)
|
9 | 27 | {
|
10 |
| - CreateHostBuilder(args).Build().Run(); |
| 28 | + var builder = WebApplication.CreateBuilder(args); |
| 29 | + ConfigureServices(builder.Services, builder.Configuration); |
| 30 | + var app = builder.Build(); |
| 31 | + Configure(app); |
| 32 | + app.Run(); |
11 | 33 | }
|
12 | 34 |
|
13 |
| - public static IHostBuilder CreateHostBuilder(string[] args) => |
14 |
| - Host.CreateDefaultBuilder(args) |
15 |
| - .ConfigureWebHostDefaults(webBuilder => |
16 |
| - { |
17 |
| - webBuilder.UseStartup<Startup>(); |
18 |
| - }); |
| 35 | + private static void ConfigureServices(IServiceCollection services, IConfiguration configuration) |
| 36 | + { |
| 37 | + services.AddDbContext<ApplicationDbContext>( |
| 38 | + options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"))); |
| 39 | + |
| 40 | + services.AddDefaultIdentity<ApplicationUser>(IdentityOptionsProvider.GetIdentityOptions) |
| 41 | + .AddRoles<ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>(); |
| 42 | + |
| 43 | + services.Configure<CookiePolicyOptions>( |
| 44 | + options => |
| 45 | + { |
| 46 | + options.CheckConsentNeeded = context => true; |
| 47 | + options.MinimumSameSitePolicy = SameSiteMode.None; |
| 48 | + }); |
| 49 | + |
| 50 | + services.AddControllersWithViews( |
| 51 | + options => |
| 52 | + { |
| 53 | + options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); |
| 54 | + }).AddRazorRuntimeCompilation(); |
| 55 | + services.AddRazorPages(); |
| 56 | + services.AddDatabaseDeveloperPageExceptionFilter(); |
| 57 | + |
| 58 | + services.AddSingleton(configuration); |
| 59 | + |
| 60 | + // Data repositories |
| 61 | + services.AddScoped(typeof(IDeletableEntityRepository<>), typeof(EfDeletableEntityRepository<>)); |
| 62 | + services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>)); |
| 63 | + services.AddScoped<IDbQueryRunner, DbQueryRunner>(); |
| 64 | + |
| 65 | + // Application services |
| 66 | + services.AddTransient<IEmailSender, NullMessageSender>(); |
| 67 | + services.AddTransient<ISettingsService, SettingsService>(); |
| 68 | + } |
| 69 | + |
| 70 | + private static void Configure(WebApplication app) |
| 71 | + { |
| 72 | + // Seed data on application startup |
| 73 | + using (var serviceScope = app.Services.CreateScope()) |
| 74 | + { |
| 75 | + var dbContext = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| 76 | + dbContext.Database.Migrate(); |
| 77 | + new ApplicationDbContextSeeder().SeedAsync(dbContext, serviceScope.ServiceProvider).GetAwaiter().GetResult(); |
| 78 | + } |
| 79 | + |
| 80 | + AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly); |
| 81 | + |
| 82 | + if (app.Environment.IsDevelopment()) |
| 83 | + { |
| 84 | + app.UseDeveloperExceptionPage(); |
| 85 | + app.UseMigrationsEndPoint(); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + app.UseExceptionHandler("/Home/Error"); |
| 90 | + app.UseHsts(); |
| 91 | + } |
| 92 | + |
| 93 | + app.UseHttpsRedirection(); |
| 94 | + app.UseStaticFiles(); |
| 95 | + app.UseCookiePolicy(); |
| 96 | + |
| 97 | + app.UseRouting(); |
| 98 | + |
| 99 | + app.UseAuthentication(); |
| 100 | + app.UseAuthorization(); |
| 101 | + |
| 102 | + app.MapControllerRoute("areaRoute", "{area:exists}/{controller=Home}/{action=Index}/{id?}"); |
| 103 | + app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); |
| 104 | + app.MapRazorPages(); |
| 105 | + } |
19 | 106 | }
|
20 | 107 | }
|
0 commit comments