简体   繁体   English

当MVC 3不止一个​​时,缓慢流式传输图像

[英]Slow streaming images in MVC 3 when more than one

I have a problem with streaming small images from database using EF5 and MVC3 我使用EF5和MVC3从数据库流式传输小图像时遇到问题

It works great when I stream 1 picture out, but when a page contains like 5 of those pictures its like glue and they take up to 5 seconds to load eventhough each are only like 5-200kb large. 当我将1张图片流出时,它的效果非常好,但是当一个页面包含5个像这样的图片时,它就像胶水一样,它们需要5秒才能加载,尽管每个图片只有5-200kb大。

在此输入图像描述

I read some post and added this to web.config 我读了一些帖子并将其添加到web.config中

<system.net> 
  <connectionManagement> 
    <add address="*" maxconnection="100" /> 
  </connectionManagement> 
</system.net> 

It didnt have any effect on my issue. 它对我的问题没有任何影响。

And using this for streaming: 并使用它进行流式传输:

 public class ImageResult : ActionResult
    {
        public ImageResult(Stream imageStream, string contentType)
        {
            if (imageStream == null)
                throw new ArgumentNullException("imageStream");
            if (contentType == null)
                throw new ArgumentNullException("contentType");

            this.ImageStream = imageStream;
            this.ContentType = contentType;
        }

        public Stream ImageStream { get; private set; }
        public string ContentType { get; private set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            HttpResponseBase response = context.HttpContext.Response;

            response.ContentType = this.ContentType;

            byte[] buffer = new byte[4096];
            while (true)
            {
                int read = this.ImageStream.Read(buffer, 0, buffer.Length);
                if (read == 0)
                    break;

                response.OutputStream.Write(buffer, 0, read);
            }

            response.End();
        }
    }

UPDATE UPDATE

I removed the ImageResult and added return File...... speeds things up, but still not acceptable speeds..... 2 seconds for 18kb file. 我删除了ImageResult并添加了返回文件......加快了速度,但仍然无法接受的速度...... 18kb文件的2秒。

Controller: 控制器:

   [SessionState(SessionStateBehavior.Disabled)]
   public class ContentController : Controller
    {
    .....
       public ActionResult Thumbnail(int fileID, int width)
       {
           var thumbnail = _fileRep.GetThumbnail(fileID, width);

           return File(thumbnail.FileContent, thumbnail.ContentType);
        }

Most probably the issue you are facing is caused by the fact that access to ASP.NET session state is exclusive per session. 很可能是您遇到的问题是由于每个会话对ASP.NET会话状态的访问是独占的。 This means that if two concurrent requests are made for the same session (by using the same SessionID value), the first request gets exclusive access to the session information. 这意味着如果对同一会话发出两个并发请求(通过使用相同的SessionID值),则第一个请求将获得对会话信息的独占访问权。 The second request executes only after the first request is finished. 第二个请求仅在第一个请求完成后执行。 You can read more about it here: ASP.NET Session State Overview ( Concurrent Requests and Session State section) 您可以在此处阅读更多相关信息: ASP.NET会话状态概述并发请求和会话状态部分)

If your actions methods for the images doesn't require access to session, you can resolve your issue by decorating the controller with SessionStateAttribute attribute: 如果图像的操作方法不需要访问会话,则可以通过使用SessionStateAttribute属性修饰控制器来解决问题:

[SessionState(SessionStateBehavior.Disabled)]

This will allow the controller to handle the requests in "parallel" way. 这将允许控制器以“并行”方式处理请求。

In case when you need read access to session, you can try using the SessionStateBehavior.ReadOnly value. 如果您需要对会话的读访问权限,可以尝试使用SessionStateBehavior.ReadOnly值。 This will not result in an exclusive lock but the request will still have to wait for a lock set by a read-write request. 这不会导致独占锁定,但请求仍然必须等待读写请求设置的锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM