ASP.NET_SessionId cookie 会在多个 GET 请求上减慢 Web 服务器的速度

发布于 2024-11-28 08:59:25 字数 1681 浏览 0 评论 0原文

我正在使用 MS VisualStudio 2010 Express 和 http://localhost 上的 ASP.NET 开发服务器测试我的 ASP.NET MVC2 Web 应用程序。 ASP.NET Framework 是版本 4。

我有一个页面,其中包含通过 Action 方法检索的图像列表,如下所示:

[HTML 代码]

<img src="/images/thumb_79c7b9f0-5939-43e5-a6d0-d5e43f4e8947.jpg" alt="image">

[Global.asax.cs 中的路由配置]

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Image",
            "images/{id}",
            new { controller = "Image", action = "Picture", id = "" }
        );
        // [...] other routing settings
    }
}

[图像控制器]

public class ImageController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Picture(string id)
    {
        try
        {
            return File(ImageBasePath + id, "image/jpeg");
        }
        catch (Exception)
        {
        }
    }

    private string ImageBasePath
    {
        get { return HttpContext.Request.PhysicalApplicationPath + WebConfigurationManager.AppSettings["dbImageBasePath"]; }
    }
}

在实践中,图片操作方法在返回图像之前执行其他检查,这就是我通过操作方法返回图像的原因。

这里的问题是,当没有会话时(即本地主机不存在 ASP.NET_SessionId cookie),浏览器获取图像的时间非常短(约 10 毫秒),而当 ASP.NET_SessionId cookie 确实存在时,时间跳到500ms-1s。这种情况发生在任何浏览器上。

我做了各种测试,发现如果我在不通过 ASP.NET 应用程序的情况下获取图像,则 ASP.NET_SessionId cookie 不会影响加载时间。

看起来多个带有 ASP.NET_SessionId cookie 的 HTTP GET 请求传递到 Web 应用程序会显着降低应用程序本身的速度。

有人对这种奇怪的行为有解释吗?

非常感谢。

更新 上述问题也发生在 IIS7 Web 服务器上,因此它并非特定于本地 ASP.NET 开发服务器。

I am testing my ASP.NET MVC2 web application using MS VisualStudio 2010 Express and the ASP.NET Development server on http://localhost. The ASP.NET Framework is version 4.

I have a page with a list of images that are retrieved through an Action method as follows:

[HTML code]

<img src="/images/thumb_79c7b9f0-5939-43e5-a6d0-d5e43f4e8947.jpg" alt="image">

[Routing configuration in Global.asax.cs]

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Image",
            "images/{id}",
            new { controller = "Image", action = "Picture", id = "" }
        );
        // [...] other routing settings
    }
}

[Image Controller]

public class ImageController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Picture(string id)
    {
        try
        {
            return File(ImageBasePath + id, "image/jpeg");
        }
        catch (Exception)
        {
        }
    }

    private string ImageBasePath
    {
        get { return HttpContext.Request.PhysicalApplicationPath + WebConfigurationManager.AppSettings["dbImageBasePath"]; }
    }
}

In practice, the Picture action method performs other checks before returning the image and this is the reason why I return the image through an action method.

The problem here is that when there is no session in place (i.e. the ASP.NET_SessionId cookie doesn't exist for localhost) the time for the browser to get the images is very short (~10ms) while when ASP.NET_SessionId cookie does exist, the time jumps to 500ms-1s. This happens on any browser.

I've done various tests and I saw that if I get the images without passing through the ASP.NET application, the ASP.NET_SessionId cookie doesn't affect the loading time.

It looks like multiple HTTP GET requests with an ASP.NET_SessionId cookie passed to the web application slow down considerably the application itself.

Does anyone have an explanation for such a strange behavior?

Many thanks.

UPDATE
The problem described above occurs on a IIS7 web server as well, so it is not specific to the local ASP.NET Development server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2024-12-05 08:59:26

ASP.NET 一次只允许一个请求访问会话状态。因此,您的所有图像请求都将被序列化,并且您会看到很长的响应时间。

解决方法是禁用会话状态或将其设置为只读。在 ASP.NET 中,这可以通过 SessionState 属性来完成:

[SessionState(SessionStateBehaviour.Disabled)]
public class ImageController : Controller
{
 ...

ASP.NET will only let a single request at a time access the session state. So all your image requests will be serialized and you're seeing long response times.

The workaround is to disable session state or set it to read-only. In ASP.NET this can be done with the SessionState attribute:

[SessionState(SessionStateBehaviour.Disabled)]
public class ImageController : Controller
{
 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文