|
1 | 1 | using System; |
2 | 2 | using JsonApiDotNetCore.Configuration; |
| 3 | +using JsonApiDotNetCore.Diagnostics; |
3 | 4 | using JsonApiDotNetCoreExample.Data; |
4 | 5 | using Microsoft.AspNetCore.Authentication; |
5 | 6 | using Microsoft.AspNetCore.Builder; |
6 | 7 | using Microsoft.AspNetCore.Hosting; |
7 | 8 | using Microsoft.EntityFrameworkCore; |
8 | 9 | using Microsoft.Extensions.Configuration; |
9 | 10 | using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.Extensions.Logging; |
10 | 12 | using Newtonsoft.Json; |
11 | 13 | using Newtonsoft.Json.Converters; |
12 | 14 |
|
13 | 15 | namespace JsonApiDotNetCoreExample.Startups |
14 | 16 | { |
15 | 17 | public sealed class Startup : EmptyStartup |
16 | 18 | { |
| 19 | + private readonly ICodeTimerSession _codeTimingSession; |
17 | 20 | private readonly string _connectionString; |
18 | 21 |
|
19 | 22 | public Startup(IConfiguration configuration) |
20 | 23 | { |
| 24 | + _codeTimingSession = new DefaultCodeTimerSession(); |
| 25 | + CodeTimingSessionManager.Capture(_codeTimingSession); |
| 26 | + |
21 | 27 | string postgresPassword = Environment.GetEnvironmentVariable("PGPASSWORD") ?? "postgres"; |
22 | 28 | _connectionString = configuration["Data:DefaultConnection"].Replace("###", postgresPassword); |
23 | 29 | } |
24 | 30 |
|
25 | 31 | // This method gets called by the runtime. Use this method to add services to the container. |
26 | 32 | public override void ConfigureServices(IServiceCollection services) |
27 | 33 | { |
28 | | - services.AddSingleton<ISystemClock, SystemClock>(); |
29 | | - |
30 | | - services.AddDbContext<AppDbContext>(options => |
| 34 | + using (CodeTimingSessionManager.Current.Measure("Configure other (startup)")) |
31 | 35 | { |
32 | | - options.UseNpgsql(_connectionString); |
| 36 | + services.AddSingleton<ISystemClock, SystemClock>(); |
| 37 | + |
| 38 | + services.AddDbContext<AppDbContext>(options => |
| 39 | + { |
| 40 | + options.UseNpgsql(_connectionString); |
33 | 41 | #if DEBUG |
34 | | - options.EnableSensitiveDataLogging(); |
35 | | - options.EnableDetailedErrors(); |
| 42 | + options.EnableSensitiveDataLogging(); |
| 43 | + options.EnableDetailedErrors(); |
36 | 44 | #endif |
37 | | - }); |
| 45 | + }); |
38 | 46 |
|
39 | | - services.AddJsonApi<AppDbContext>(options => |
40 | | - { |
41 | | - options.Namespace = "api/v1"; |
42 | | - options.UseRelativeLinks = true; |
43 | | - options.ValidateModelState = true; |
44 | | - options.IncludeTotalResourceCount = true; |
45 | | - options.SerializerSettings.Formatting = Formatting.Indented; |
46 | | - options.SerializerSettings.Converters.Add(new StringEnumConverter()); |
| 47 | + using (CodeTimingSessionManager.Current.Measure("Configure JSON:API (startup)")) |
| 48 | + { |
| 49 | + services.AddJsonApi<AppDbContext>(options => |
| 50 | + { |
| 51 | + options.Namespace = "api/v1"; |
| 52 | + options.UseRelativeLinks = true; |
| 53 | + options.ValidateModelState = true; |
| 54 | + options.IncludeTotalResourceCount = true; |
| 55 | + options.SerializerSettings.Formatting = Formatting.Indented; |
| 56 | + options.SerializerSettings.Converters.Add(new StringEnumConverter()); |
47 | 57 | #if DEBUG |
48 | | - options.IncludeExceptionStackTraceInErrors = true; |
| 58 | + options.IncludeExceptionStackTraceInErrors = true; |
49 | 59 | #endif |
50 | | - }, discovery => discovery.AddCurrentAssembly()); |
| 60 | + }, discovery => discovery.AddCurrentAssembly()); |
| 61 | + } |
| 62 | + } |
51 | 63 | } |
52 | 64 |
|
53 | 65 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
54 | | - public override void Configure(IApplicationBuilder app, IWebHostEnvironment environment) |
| 66 | + public override void Configure(IApplicationBuilder app, IWebHostEnvironment environment, ILoggerFactory loggerFactory) |
55 | 67 | { |
56 | | - using (IServiceScope scope = app.ApplicationServices.CreateScope()) |
| 68 | + ILogger<Startup> logger = loggerFactory.CreateLogger<Startup>(); |
| 69 | + |
| 70 | + using (CodeTimingSessionManager.Current.Measure("Initialize other (startup)")) |
| 71 | + { |
| 72 | + using (IServiceScope scope = app.ApplicationServices.CreateScope()) |
| 73 | + { |
| 74 | + var appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 75 | + appDbContext.Database.EnsureCreated(); |
| 76 | + } |
| 77 | + |
| 78 | + app.UseRouting(); |
| 79 | + |
| 80 | + using (CodeTimingSessionManager.Current.Measure("Initialize JSON:API (startup)")) |
| 81 | + { |
| 82 | + app.UseJsonApi(); |
| 83 | + } |
| 84 | + |
| 85 | + app.UseEndpoints(endpoints => endpoints.MapControllers()); |
| 86 | + } |
| 87 | + |
| 88 | + if (CodeTimingSessionManager.IsEnabled) |
57 | 89 | { |
58 | | - var appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
59 | | - appDbContext.Database.EnsureCreated(); |
| 90 | + string timingResults = CodeTimingSessionManager.Current.GetResults(); |
| 91 | + logger.LogInformation($"Measurement results for application startup:{Environment.NewLine}{timingResults}"); |
60 | 92 | } |
61 | 93 |
|
62 | | - app.UseRouting(); |
63 | | - app.UseJsonApi(); |
64 | | - app.UseEndpoints(endpoints => endpoints.MapControllers()); |
| 94 | + _codeTimingSession.Dispose(); |
65 | 95 | } |
66 | 96 | } |
67 | 97 | } |
0 commit comments