title: In-process hosting on .NET Core 2.2 slowed down API response time
body: <p>I have recently upgraded .NET Core web API from 2.1 to 2.2. Microsoft claims that the new InProcess hosting model should be <a href="https://devblogs.microsoft.com/aspnet/asp-net-core-2-2-0-preview2-now-available/#asp.net-core-module-improvements" rel="noreferrer">up to 4x faster</a> than OutOfProcess hosting model. </p>
  
  <p>However, when I set IIS hosting model to <a href="https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-2.2#in-process-hosting-model" rel="noreferrer">InProcess</a> <code>&lt;AspNetCoreHostingModel&gt;InProcess&lt;/AspNetCoreHostingModel&gt;</code> in .csproj, average API response is 50 - 100ms higher than when I set <code>&lt;AspNetCoreHostingModel&gt;OutOfProcess&lt;/AspNetCoreHostingModel&gt;</code>.
  Any idea why it's slowing down the API response time (higher ping)?</p>
  
  <p>My Startup.cs:</p>
  
  <pre><code>public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              IoCContainer.Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              //Add proper cookie request to follow GDPR
              services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
              {
                  // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                  options.CheckConsentNeeded = context =&gt; true;
                  options.MinimumSameSitePolicy = SameSiteMode.None;
              });
  
              //Add SendGrid email sender
              services.AddSendGridEmailSender();
  
              //Add general email template sender
              services.AddEmailTemplateSender();
  
              //Add ExcelFileDownloadService
              services.AddTransient&lt;IExcelFileDownloadService, ExcelFileDownloadService&gt;();
              //Add ExcelFileUploadService
              services.AddTransient&lt;IExcelFileUploadService, ExcelFileUploadService&gt;();
              //Add FileUploadService
              services.AddTransient&lt;IUploadFileService, UploadFileService&gt;();
  
              //CORS
              services.AddCors();
  
              //Add ApplicationDbContext to DI using MSSQL
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(IoCContainer.Configuration.GetConnectionString("AspifyConnection")));
  
              //Store keys to File System
              string path = Directory.GetCurrentDirectory() + @"\keys\";
              services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(path));
  
  
              //Add RepositoryWrapper to DI
              services.AddScoped&lt;IRepositoryWrapper, RepositoryWrapper&gt;();
  
              //Adds cookie based authentication
              //Adds classes like UserManager, SignInManager, PasswordHashers etc.
              services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;()
                  .AddUserManager&lt;UserManager&lt;ApplicationUser&gt;&gt;()
                  //Adds UserStore and RoleStore from this context
                  .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
                  //Adds a provider that generates unique keys and hashes
                  .AddDefaultTokenProviders();
  
  
              //Add JWT authentication for API clients
              var key = Encoding.ASCII.GetBytes(IoCContainer.Configuration["JwtSecretKey"]);
              services.AddAuthentication(options =&gt;
              {
                  options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                  options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
              })
              .AddJwtBearer(options =&gt;
              {
                  options.RequireHttpsMetadata = false;
                  options.SaveToken = true;
                  options.TokenValidationParameters = new TokenValidationParameters
                  {
                      ValidateIssuerSigningKey = true,
                      IssuerSigningKey = new SymmetricSecurityKey(key),
                      ValidateIssuer = false,
                      ValidateAudience = false
                  };
              });
  
              services.AddAuthorization(options =&gt;
              {
                  options.AddPolicy("RequireAdministratorRole", policy =&gt; policy.RequireRole("Administrator"));
              });
  
              //Change Identity Framework password policy
              services.Configure&lt;IdentityOptions&gt;(options =&gt;
              {
                  options.Password.RequireDigit = false;
                  options.Password.RequiredLength = 5;
                  options.Password.RequireLowercase = true;
                  options.Password.RequireUppercase = false;
                  options.Password.RequireNonAlphanumeric = false;
              });
  
              services.AddAntiforgery(options =&gt; options.HeaderName = "X-XSRF-TOKEN");
  
              services.AddMvc(options =&gt;
              {
  
              }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
          {
              //Setup identity
              app.UseAuthentication();
  
              UpdateDatabase(app);
  
              //Allow all CORS
              app.UseCors(x =&gt; x
                  .AllowAnyOrigin()
                  .AllowAnyMethod()
                  .AllowAnyHeader()
                  .AllowCredentials());
  
              app.UseDeveloperExceptionPage();
  
              //Force non-essential cookies to only store if the user has consented
              app.UseCookiePolicy();
  
              //Enable static files in wwwroot folder
              app.UseStaticFiles();
  
              //set HTTP routes
              app.UseMvc(routes =&gt;
              {
  
                  //Api route
                  routes.MapRoute(
                  name: "api",
                  template: "api/{controller}/{action}/{moreInfo?}");
  
                  //Default route to React
                  routes.MapRoute(
                      name: "default",
                      template: "{*path}",
                      defaults: new { controller = "Home", action = "Index" });
              });
          }
  
          private static void UpdateDatabase(IApplicationBuilder app)
          {
              using (var serviceScope = app.ApplicationServices
                  .GetRequiredService&lt;IServiceScopeFactory&gt;()
                  .CreateScope())
              {
                  using (var context = serviceScope.ServiceProvider.GetService&lt;ApplicationDbContext&gt;())
                  {
                      context.Database.EnsureCreated();
                  }
              }
          }
      }
  </code></pre>
  
  <p>.csproj:</p>
  
  <pre><code> &lt;Project Sdk="Microsoft.NET.Sdk.Web"&gt;
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netcoreapp2.2&lt;/TargetFramework&gt;
      &lt;RuntimeFrameworkVersion&gt;2.2.0&lt;/RuntimeFrameworkVersion&gt;
      &lt;AspNetCoreHostingModel&gt;OutOfProcess&lt;/AspNetCoreHostingModel&gt;
      &lt;GenerateRuntimeConfigurationFiles&gt;true&lt;/GenerateRuntimeConfigurationFiles&gt;
      &lt;OutputType&gt;Exe&lt;/OutputType&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Compile Remove="Migrations\20181110121254_rentalInfoLeasingTypeSetNull.cs" /&gt;
      &lt;Compile Remove="Migrations\20181110121254_rentalInfoLeasingTypeSetNull.Designer.cs" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;PackageReference Include="EPPlus.Core" Version="1.5.4" /&gt;
      &lt;PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /&gt;
      &lt;PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" /&gt;
      &lt;PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="2.2.0" /&gt;
      &lt;PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" /&gt;
      &lt;PackageReference Include="Sendgrid" Version="9.10.0" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Folder Include="wwwroot\Examples\" /&gt;
      &lt;Folder Include="wwwroot\Uploads\" /&gt;
      &lt;Folder Include="wwwroot\EmailTemplates\" /&gt;
      &lt;Folder Include="wwwroot\React" /&gt;
      &lt;Folder Include="wwwroot\React\media" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;None Include="Views\Home\Index.cshtml" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ProjectExtensions&gt;&lt;VisualStudio&gt;&lt;UserProperties appsettings_1json__JSONSchema="" /&gt;&lt;/VisualStudio&gt;&lt;/ProjectExtensions&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>CompaniesController.cs:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Get list of all companies without their domains
      /// &lt;/summary&gt;
      /// &lt;returns&gt;List of companies&lt;/returns&gt;
      [Authorize(Roles = "Admin")]
      [HttpGet]
      public async Task&lt;IActionResult&gt; Get()
      {
          List&lt;Company&gt; companies = await _repoWrapper.Companies.GetAllAsync();
          List&lt;CompanyListItemResponse&gt; response = new List&lt;CompanyListItemResponse&gt;();
  
          foreach (Company company in companies)
          {
              response.Add(new CompanyListItemResponse { Id = company.Id, Name = company.Name });
          }
  
          response = response.OrderBy(x =&gt; x.Name).ToList();
  
          return Ok(response);
      }
  </code></pre>