Startup.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Data.Common;
  5. using System.Data.SqlClient;
  6. using Benchmarks.Configuration;
  7. using Benchmarks.Data;
  8. using Benchmarks.Middleware;
  9. using Microsoft.AspNetCore.Builder;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.PlatformAbstractions;
  14. namespace Benchmarks
  15. {
  16. public class Startup
  17. {
  18. public Startup(IHostingEnvironment hostingEnv, Scenarios scenarios)
  19. {
  20. // Set up configuration sources.
  21. var builder = new ConfigurationBuilder()
  22. .SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
  23. .AddCommandLine(Program.Args)
  24. .AddJsonFile("appsettings.json")
  25. .AddJsonFile($"appsettings.{hostingEnv.EnvironmentName}.json", optional: true)
  26. .AddEnvironmentVariables();
  27. Configuration = builder.Build();
  28. Scenarios = scenarios;
  29. }
  30. public IConfigurationRoot Configuration { get; set; }
  31. public Scenarios Scenarios { get; }
  32. public void ConfigureServices(IServiceCollection services)
  33. {
  34. services.Configure<AppSettings>(Configuration);
  35. // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the
  36. // registration done in Program.Main
  37. services.AddSingleton(Scenarios);
  38. // Common DB services
  39. services.AddSingleton<IRandom, DefaultRandom>();
  40. services.AddSingleton<ApplicationDbSeeder>();
  41. services.AddEntityFrameworkSqlServer()
  42. .AddDbContext<ApplicationDbContext>();
  43. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  44. {
  45. // TODO: Add support for plugging in different DbProviderFactory implementations via configuration
  46. services.AddSingleton<DbProviderFactory>(SqlClientFactory.Instance);
  47. }
  48. if (Scenarios.Any("Ef"))
  49. {
  50. services.AddScoped<EfDb>();
  51. }
  52. if (Scenarios.Any("Raw"))
  53. {
  54. services.AddScoped<RawDb>();
  55. }
  56. if (Scenarios.Any("Dapper"))
  57. {
  58. services.AddScoped<DapperDb>();
  59. }
  60. if (Scenarios.Any("Fortunes"))
  61. {
  62. services.AddWebEncoders();
  63. }
  64. if (Scenarios.Any("Mvc"))
  65. {
  66. var mvcBuilder = services
  67. .AddMvcCore()
  68. //.AddApplicationPart(typeof(Startup).GetTypeInfo().Assembly)
  69. .AddControllersAsServices();
  70. if (Scenarios.MvcJson)
  71. {
  72. mvcBuilder.AddJsonFormatters();
  73. }
  74. if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes"))
  75. {
  76. mvcBuilder
  77. .AddViews()
  78. .AddRazorViewEngine();
  79. }
  80. }
  81. }
  82. public void Configure(IApplicationBuilder app, ApplicationDbSeeder dbSeeder, ApplicationDbContext dbContext)
  83. {
  84. app.UseErrorHandler();
  85. if (Scenarios.Plaintext)
  86. {
  87. app.UsePlainText();
  88. }
  89. if (Scenarios.Json)
  90. {
  91. app.UseJson();
  92. }
  93. // Single query endpoints
  94. if (Scenarios.DbSingleQueryRaw)
  95. {
  96. app.UseSingleQueryRaw();
  97. }
  98. if (Scenarios.DbSingleQueryDapper)
  99. {
  100. app.UseSingleQueryDapper();
  101. }
  102. if (Scenarios.DbSingleQueryEf)
  103. {
  104. app.UseSingleQueryEf();
  105. }
  106. // Multiple query endpoints
  107. if (Scenarios.DbMultiQueryRaw)
  108. {
  109. app.UseMultipleQueriesRaw();
  110. }
  111. if (Scenarios.DbMultiQueryDapper)
  112. {
  113. app.UseMultipleQueriesDapper();
  114. }
  115. if (Scenarios.DbMultiQueryEf)
  116. {
  117. app.UseMultipleQueriesEf();
  118. }
  119. // Fortunes endpoints
  120. if (Scenarios.DbFortunesRaw)
  121. {
  122. app.UseFortunesRaw();
  123. }
  124. if (Scenarios.DbFortunesDapper)
  125. {
  126. app.UseFortunesDapper();
  127. }
  128. if (Scenarios.DbFortunesEf)
  129. {
  130. app.UseFortunesEf();
  131. }
  132. if (Scenarios.Any("Db"))
  133. {
  134. dbContext.Database.EnsureCreated();
  135. if (!dbSeeder.Seed())
  136. {
  137. Environment.Exit(1);
  138. }
  139. }
  140. if (Scenarios.Any("Mvc"))
  141. {
  142. app.UseMvc();
  143. }
  144. if (Scenarios.StaticFiles)
  145. {
  146. app.UseStaticFiles();
  147. }
  148. app.RunDebugInfoPage();
  149. }
  150. }
  151. }