【问题标题】:First query on ASP.NET Web API on Azure is slowAzure 上 ASP.NET Web API 的第一次查询很慢
【发布时间】:2015-01-09 14:11:28
【问题描述】:

我正在尝试设置一个 ASP.NET Web API 并在 Azure 上运行它。服务启动后的第一次查询总是很慢。之后的任何查询都可以。

我尝试将以下代码添加到我的项目中,但它并没有提高第一个查询的性能。它来自这个关于类似主题的答案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

我还使用了以下步骤来确保我应该能够使用 ServerManager

  • 参考 C:\Windows\system32\inetsrv\Microsoft.Web.Administration.dll(或通过 NuGet 获得)
  • 在 WebRole 元素下的服务定义中添加 Runtime executionContext="elevated"

在尝试自动启动 Web API 时,我是否遗漏了什么?

【问题讨论】:

    标签: asp.net vb.net azure asp.net-web-api azure-web-roles


    【解决方案1】:

    你在使用实体框架吗?如果是这样,您看到的性能影响可能来自第一次调用 DbSet 时,EF 必须构建模型。如果您希望在调用服务之前发生这种情况,您可以从您的启动例程中进行任何 EF 调用。

    【讨论】:

    • 我正在使用实体框架。我会试一试,看看效果如何。
    • 这似乎没有解决问题。我正在使用 EF 6.1 并且还使用 T4 模板,因此在启动过程中 EF 应该不是问题。我也尝试过回收应用程序池,这有助于在应用程序启动后进行查询,但第一次查询仍然很慢。
    猜你喜欢
    • 2019-12-12
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    相关资源
    最近更新 更多