0

I am trying to set up an ASP.NET Web API and run it on Azure. The first query after the service has started is always slow. Any queries afterwards are fine.

I've tried adding the following code to my project but it hasn't improved the performance of the first query. It came from this answer on a similar topic AutoStart a WCF on Azure WebRole

Public Class WebRole
    Inherits RoleEntryPoint
    Public Overrides Sub Run()
        Using serverManager = New ServerManager()
            Dim mainSite = serverManager.Sites(RoleEnvironment.CurrentRoleInstance.Id + "_Web")
            Dim mainApplication = mainSite.Applications("/")
            Dim mainApplicationPool = serverManager.ApplicationPools(mainApplication.ApplicationPoolName)
            mainApplicationPool("autoStart") = True
            mainApplicationPool("startMode") = "AlwaysRunning"

            serverManager.CommitChanges()
        End Using

        MyBase.Run()
    End Sub

    Public Overrides Function OnStart() As Boolean

        Try

            Using svrManager As New ServerManager

                Dim appPoolName = svrManager.Sites.First.Applications.First.ApplicationPoolName
                Dim appPool = svrManager.ApplicationPools(appPoolName)
                appPool.ProcessModel.IdleTimeout = TimeSpan.Zero
                appPool.Recycling.PeriodicRestart.Time = TimeSpan.Zero

                svrManager.CommitChanges()

            End Using

        Catch ex As Exception
        End Try

        Return MyBase.OnStart()

    End Function
End Class

I've also made used the following steps to ensure that I should be able to use ServerManager

  • reference C:\Windows\system32\inetsrv\Microsoft.Web.Administration.dll (or available through NuGet)
  • add Runtime executionContext="elevated" in your Service Definition under the WebRole element

Is there something that I am missing in this attempt to auto start the Web API?

Community
  • 1
  • 1
Tom
  • 365
  • 3
  • 19

1 Answers1

1

Are you using Entity Framework? If so, the performance hit you're seeing may be from the first time your DbSet is called, EF has to build the model. If you'd like for this to happen before a call is made to the service, you can just make any EF call from your Startup routine.

esmoore68
  • 1,276
  • 1
  • 8
  • 16
  • I am using Entity Framework. I'll give this a try and see how it goes. – Tom Jan 09 '15 at 14:55
  • This doesn't seem to have fixed the issue. I am using EF 6.1 and also using the T4 templates so EF should not be a problem when it comes to start up process. I have also tried recycling the app pool and this helps with queries once the app has started but this first query remains slow. – Tom Jan 19 '15 at 15:51