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?