Startup.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 System.Text.Encodings.Web;
  7. using System.Text.Unicode;
  8. using Benchmarks.Configuration;
  9. using Benchmarks.Data;
  10. using Benchmarks.Middleware;
  11. using Microsoft.AspNetCore.Builder;
  12. using Microsoft.AspNetCore.Hosting;
  13. using Microsoft.Extensions.Configuration;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Microsoft.Extensions.Options;
  16. using Microsoft.Extensions.PlatformAbstractions;
  17. using Npgsql;
  18. namespace Benchmarks
  19. {
  20. public class Startup
  21. {
  22. public Startup(IHostingEnvironment hostingEnv, Scenarios scenarios)
  23. {
  24. // Set up configuration sources.
  25. var builder = new ConfigurationBuilder()
  26. .SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
  27. .AddCommandLine(Program.Args)
  28. .AddJsonFile("appsettings.json")
  29. .AddJsonFile($"appsettings.{hostingEnv.EnvironmentName}.json", optional: true)
  30. .AddEnvironmentVariables();
  31. Configuration = builder.Build();
  32. Scenarios = scenarios;
  33. }
  34. public IConfigurationRoot Configuration { get; set; }
  35. public Scenarios Scenarios { get; }
  36. public void ConfigureServices(IServiceCollection services)
  37. {
  38. services.Configure<AppSettings>(Configuration);
  39. // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the
  40. // registration done in Program.Main
  41. services.AddSingleton(Scenarios);
  42. // Common DB services
  43. services.AddSingleton<IRandom, DefaultRandom>();
  44. services.AddSingleton<ApplicationDbSeeder>();
  45. services.AddEntityFrameworkSqlServer()
  46. .AddDbContext<ApplicationDbContext>();
  47. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  48. {
  49. services.AddSingleton<DbProviderFactory>((provider) => {
  50. var settings = provider.GetRequiredService<IOptions<AppSettings>>().Value;
  51. if (settings.Database == DatabaseServer.PostgreSql)
  52. {
  53. return NpgsqlFactory.Instance;
  54. }
  55. else
  56. {
  57. return SqlClientFactory.Instance;
  58. }
  59. });
  60. }
  61. if (Scenarios.Any("Ef"))
  62. {
  63. services.AddScoped<EfDb>();
  64. }
  65. if (Scenarios.Any("Raw"))
  66. {
  67. services.AddScoped<RawDb>();
  68. }
  69. if (Scenarios.Any("Dapper"))
  70. {
  71. services.AddScoped<DapperDb>();
  72. }
  73. if (Scenarios.Any("Fortunes"))
  74. {
  75. var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana);
  76. settings.AllowCharacter('\u2014'); // allow EM DASH through
  77. services.AddWebEncoders((options) =>
  78. {
  79. options.TextEncoderSettings = settings;
  80. });
  81. }
  82. if (Scenarios.Any("Mvc"))
  83. {
  84. var mvcBuilder = services
  85. .AddMvcCore()
  86. //.AddApplicationPart(typeof(Startup).GetTypeInfo().Assembly)
  87. .AddControllersAsServices();
  88. if (Scenarios.MvcJson || Scenarios.Any("MvcDbSingle") || Scenarios.Any("MvcDbMulti"))
  89. {
  90. mvcBuilder.AddJsonFormatters();
  91. }
  92. if (Scenarios.MvcViews || Scenarios.Any("MvcDbFortunes"))
  93. {
  94. mvcBuilder
  95. .AddViews()
  96. .AddRazorViewEngine();
  97. }
  98. }
  99. }
  100. public void Configure(IApplicationBuilder app, ApplicationDbSeeder dbSeeder, ApplicationDbContext dbContext)
  101. {
  102. if (Scenarios.Plaintext)
  103. {
  104. app.UsePlainText();
  105. }
  106. if (Scenarios.Json)
  107. {
  108. app.UseJson();
  109. }
  110. // Single query endpoints
  111. if (Scenarios.DbSingleQueryRaw)
  112. {
  113. app.UseSingleQueryRaw();
  114. }
  115. if (Scenarios.DbSingleQueryDapper)
  116. {
  117. app.UseSingleQueryDapper();
  118. }
  119. if (Scenarios.DbSingleQueryEf)
  120. {
  121. app.UseSingleQueryEf();
  122. }
  123. // Multiple query endpoints
  124. if (Scenarios.DbMultiQueryRaw)
  125. {
  126. app.UseMultipleQueriesRaw();
  127. }
  128. if (Scenarios.DbMultiQueryDapper)
  129. {
  130. app.UseMultipleQueriesDapper();
  131. }
  132. if (Scenarios.DbMultiQueryEf)
  133. {
  134. app.UseMultipleQueriesEf();
  135. }
  136. // Multiple update endpoints
  137. if (Scenarios.DbMultiUpdateRaw)
  138. {
  139. app.UseMultipleUpdatesRaw();
  140. }
  141. if (Scenarios.DbMultiUpdateDapper)
  142. {
  143. app.UseMultipleUpdatesDapper();
  144. }
  145. if (Scenarios.DbMultiUpdateEf)
  146. {
  147. app.UseMultipleUpdatesEf();
  148. }
  149. // Fortunes endpoints
  150. if (Scenarios.DbFortunesRaw)
  151. {
  152. app.UseFortunesRaw();
  153. }
  154. if (Scenarios.DbFortunesDapper)
  155. {
  156. app.UseFortunesDapper();
  157. }
  158. if (Scenarios.DbFortunesEf)
  159. {
  160. app.UseFortunesEf();
  161. }
  162. if (Scenarios.Any("Db"))
  163. {
  164. dbContext.Database.EnsureCreated();
  165. if (!dbSeeder.Seed())
  166. {
  167. Environment.Exit(1);
  168. }
  169. }
  170. if (Scenarios.Any("Mvc"))
  171. {
  172. app.UseMvc();
  173. }
  174. if (Scenarios.StaticFiles)
  175. {
  176. app.UseStaticFiles();
  177. }
  178. app.RunDebugInfoPage();
  179. }
  180. }
  181. }