Tom
Tom

Reputation: 377

First query on ASP.NET Web API on Azure is slow

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

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

Upvotes: 0

Views: 220

Answers (1)

esmoore68
esmoore68

Reputation: 1296

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.

Upvotes: 1

Related Questions