Startup.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.EntityFrameworkCore;
  14. using Microsoft.Extensions.Configuration;
  15. using Microsoft.Extensions.DependencyInjection;
  16. using Npgsql;
  17. namespace Benchmarks
  18. {
  19. public class Startup
  20. {
  21. public Startup(IHostingEnvironment hostingEnv, Scenarios scenarios)
  22. {
  23. // Set up configuration sources.
  24. var builder = new ConfigurationBuilder()
  25. .SetBasePath(hostingEnv.ContentRootPath)
  26. .AddCommandLine(Program.Args)
  27. .AddJsonFile("appsettings.json")
  28. .AddJsonFile($"appsettings.{hostingEnv.EnvironmentName}.json", optional: true)
  29. .AddEnvironmentVariables();
  30. Configuration = builder.Build();
  31. Scenarios = scenarios;
  32. }
  33. public IConfigurationRoot Configuration { get; set; }
  34. public Scenarios Scenarios { get; }
  35. public void ConfigureServices(IServiceCollection services)
  36. {
  37. services.Configure<AppSettings>(Configuration);
  38. // We re-register the Scenarios as an instance singleton here to avoid it being created again due to the
  39. // registration done in Program.Main
  40. services.AddSingleton(Scenarios);
  41. // Common DB services
  42. services.AddSingleton<IRandom, DefaultRandom>();
  43. services.AddSingleton<ApplicationDbSeeder>();
  44. services.AddEntityFrameworkSqlServer();
  45. var appSettings = Configuration.Get<AppSettings>();
  46. if (appSettings.Database == DatabaseServer.PostgreSql)
  47. {
  48. services.AddDbContextPool<ApplicationDbContext>(options => options.UseNpgsql(appSettings.ConnectionString));
  49. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  50. {
  51. services.AddSingleton<DbProviderFactory>(NpgsqlFactory.Instance);
  52. }
  53. }
  54. else
  55. {
  56. services.AddDbContextPool<ApplicationDbContext>(options => options.UseSqlServer(appSettings.ConnectionString));
  57. if (Scenarios.Any("Raw") || Scenarios.Any("Dapper"))
  58. {
  59. services.AddSingleton<DbProviderFactory>(SqlClientFactory.Instance);
  60. }
  61. }
  62. if (Scenarios.Any("Ef"))
  63. {
  64. services.AddScoped<EfDb>();
  65. }
  66. if (Scenarios.Any("Raw"))
  67. {
  68. services.AddScoped<RawDb>();
  69. }
  70. if (Scenarios.Any("Dapper"))
  71. {
  72. services.AddScoped<DapperDb>();
  73. }
  74. if (Scenarios.Any("Fortunes"))
  75. {
  76. var settings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Katakana, UnicodeRanges.Hiragana);
  77. settings.AllowCharacter('\u2014'); // allow EM DASH through
  78. services.AddWebEncoders((options) =>
  79. {
  80. options.TextEncoderSettings = settings;
  81. });
  82. }
  83. if (Scenarios.Any("Mvc"))
  84. {
  85. var mvcBuilder = services
  86. .AddMvcCore()
  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)
  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. if (!dbSeeder.Seed())
  165. {
  166. Environment.Exit(1);
  167. }
  168. }
  169. if (Scenarios.Any("Mvc"))
  170. {
  171. app.UseMvc();
  172. }
  173. if (Scenarios.StaticFiles)
  174. {
  175. app.UseStaticFiles();
  176. }
  177. app.RunDebugInfoPage();
  178. }
  179. }
  180. }