title: Poor Azure App Service performance using OWIN?
body: <p>We recently deployed a .NET Framework 4.8 Web API 2 application to an Azure App Service.  We’re using the Azure S1 application service plan.</p>
  
  <p>Unfortunately, we’re seeing very inconsistent response times when making API requests to this service – sometimes as long as 10 seconds.  The same application running on a VM takes less than a second.</p>
  
  <p>Initially we assumed it was slow database query performance, but after doing some profiling it appears the queries actually run quickly and most of the time appears to be in “external code”.</p>
  
  <p>Specifically, it appears that most of the time is spent in the OWIN request handler code:</p>
  
  <p><img src="https://i.imgur.com/T4QOtvx.png" alt="profiler"></p>
  
  <p>Here is our Startup.cs:</p>
  
  <pre><code>app.UseCors(CorsOptions.AllowAll);
  app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions() {
      AllowInsecureHttp = true,
      TokenEndpointPath = new PathString("/token"),
      AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(AccessTokenExpirationInMinutes),
      Provider = new CustomOAuthAuthorizationProvider()
  });
  app.UseCustomSessionAuthentication(new CustomSessionAuthenticationOptions());
  app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
  
  var config = GlobalConfiguration.Configuration;
  
  WebApiConfig.Register(config);
  ServiceConfig.Register(config);
  
  app.UseWebApi(config);
  </code></pre>
  
  <p>Has anyone experienced issues using OWIN in an Azure App Service?  Is there special configuration or code changes needed?</p>
  
  <p>Versions:</p>
  
  <ul>
  <li>.NET Framework 4.8</li>
  <li>MVC 5.2.3</li>
  <li>Microsoft.Owin.* 3.0.1</li>
  </ul>