|
1 | | -using Ardalis.ListStartupServices; |
2 | | -using Autofac; |
3 | | -using Autofac.Extensions.DependencyInjection; |
| 1 | +using System.Reflection; |
| 2 | +using Ardalis.ListStartupServices; |
| 3 | +using Ardalis.SharedKernel; |
4 | 4 | using NimblePros.SampleToDo.Core; |
5 | 5 | using NimblePros.SampleToDo.Infrastructure; |
6 | 6 | using NimblePros.SampleToDo.Infrastructure.Data; |
7 | 7 | using NimblePros.SampleToDo.Web; |
8 | 8 | using FastEndpoints; |
9 | 9 | using FastEndpoints.Swagger; |
10 | 10 | using FastEndpoints.ApiExplorer; |
| 11 | +using MediatR; |
11 | 12 | using Serilog; |
12 | 13 | using Microsoft.EntityFrameworkCore; |
| 14 | +using NimblePros.SampleToDo.Core.ProjectAggregate; |
| 15 | +using NimblePros.SampleToDo.UseCases.Contributors.Create; |
| 16 | +using Serilog.Extensions.Logging; |
13 | 17 |
|
14 | | -var builder = WebApplication.CreateBuilder(args); |
| 18 | +var logger = Log.Logger = new LoggerConfiguration() |
| 19 | + .Enrich.FromLogContext() |
| 20 | + .WriteTo.Console() |
| 21 | + .CreateLogger(); |
| 22 | + |
| 23 | +logger.Information("Starting web host"); |
15 | 24 |
|
16 | | -builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); |
| 25 | +var builder = WebApplication.CreateBuilder(args); |
17 | 26 |
|
18 | 27 | builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration)); |
| 28 | +var microsoftLogger = new SerilogLoggerFactory(logger) |
| 29 | + .CreateLogger<Program>(); |
19 | 30 |
|
20 | 31 | builder.Services.Configure<CookiePolicyOptions>(options => |
21 | 32 | { |
|
43 | 54 | // c.OperationFilter<FastEndpointsOperationFilter>(); |
44 | 55 | //}); |
45 | 56 |
|
| 57 | +builder.Services.AddCoreServices(microsoftLogger); |
| 58 | +builder.Services.AddInfrastructureServices(microsoftLogger, builder.Environment.IsDevelopment()); |
| 59 | + |
| 60 | +ConfigureMediatR(); |
| 61 | + |
46 | 62 | // add list services for diagnostic purposes - see https://github.com/ardalis/AspNetCoreStartupServices |
47 | 63 | builder.Services.Configure<ServiceConfig>(config => |
48 | 64 | { |
|
53 | 69 | }); |
54 | 70 |
|
55 | 71 |
|
56 | | -builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder => |
57 | | -{ |
58 | | - containerBuilder.RegisterModule(new DefaultCoreModule()); |
59 | | - containerBuilder.RegisterModule(new AutofacInfrastructureModule(builder.Environment.IsDevelopment())); |
60 | | -}); |
61 | | - |
62 | 72 | var app = builder.Build(); |
63 | 73 |
|
64 | 74 | if (app.Environment.IsDevelopment()) |
|
80 | 90 | //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1")); |
81 | 91 |
|
82 | 92 | // Seed Database |
83 | | -using (var scope = app.Services.CreateScope()) |
84 | | -{ |
85 | | - var services = scope.ServiceProvider; |
| 93 | +SeedDatabase(app); |
86 | 94 |
|
87 | | - try |
| 95 | +app.Run(); |
| 96 | + |
| 97 | + |
| 98 | +void ConfigureMediatR() |
| 99 | +{ |
| 100 | + var mediatRAssemblies = new[] |
88 | 101 | { |
89 | | - var context = services.GetRequiredService<AppDbContext>(); |
90 | | - // context.Database.Migrate(); |
91 | | - context.Database.EnsureCreated(); |
92 | | - SeedData.Initialize(services); |
93 | | - } |
94 | | - catch (Exception ex) |
| 102 | + Assembly.GetAssembly(typeof(Project)), // Core |
| 103 | + Assembly.GetAssembly(typeof(CreateContributorCommand)), // UseCases |
| 104 | + Assembly.GetAssembly(typeof(InfrastructureServiceExtensions)) // Infrastructure |
| 105 | + }; |
| 106 | + |
| 107 | + builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(mediatRAssemblies!)); |
| 108 | + builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)); |
| 109 | + builder.Services.AddScoped<IDomainEventDispatcher, MediatRDomainEventDispatcher>(); |
| 110 | +} |
| 111 | + |
| 112 | +void SeedDatabase(WebApplication app) |
| 113 | +{ |
| 114 | + using (var scope = app.Services.CreateScope()) |
95 | 115 | { |
96 | | - var logger = services.GetRequiredService<ILogger<Program>>(); |
97 | | - logger.LogError(ex, "An error occurred seeding the DB. {exceptionMessage}", ex.Message); |
| 116 | + var services = scope.ServiceProvider; |
| 117 | + |
| 118 | + try |
| 119 | + { |
| 120 | + var context = services.GetRequiredService<AppDbContext>(); |
| 121 | + // context.Database.Migrate(); |
| 122 | + context.Database.EnsureCreated(); |
| 123 | + SeedData.Initialize(services); |
| 124 | + } |
| 125 | + catch (Exception ex) |
| 126 | + { |
| 127 | + var logger = services.GetRequiredService<ILogger<Program>>(); |
| 128 | + logger.LogError(ex, "An error occurred seeding the DB. {exceptionMessage}", ex.Message); |
| 129 | + } |
98 | 130 | } |
99 | 131 | } |
100 | 132 |
|
101 | | -app.Run(); |
102 | | - |
103 | 133 | // Make the implicit Program.cs class public, so integration tests can reference the correct assembly for host building |
104 | 134 | public partial class Program |
105 | 135 | { |
|
0 commit comments