ashx处理程序,访问void内部的HttpContext.Current

时间:2011-08-13 15:29:57

标签: c# stream ashx mjpeg aforge

我已经戏剧性地改变了这个问题但是在这里。我正在从IP摄像头读取mjpeg流,一切顺利。但现在我收到的每一帧(请参阅stream_NewFrame)我想将此图像推送到客户端。但我似乎无法弄清楚如何访问HttpContext.Current导致它在该函数内始终为null。有没有人知道如何访问HttpContext上下文就像我在ProcessRequest函数中可以做的那样?我在这里遗漏了一些明显的东西但是我想不出来了!谢谢你的时间。

  public class ImageHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {

        //Get parameter
        string Url = context.Request.QueryString["url"];
        string Username = context.Request.QueryString["username"];
        string Password = context.Request.QueryString["password"];

        //Set cache 
        HttpResponse Response = HttpContext.Current.Response;
        Response.Expires = 0;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "multipart/x-mixed-replace";

        // create MJPEG video source
        MJPEGStream stream = new MJPEGStream(string.Format("{0}/video.cgi?user={1}&pwd={2}", Url, Username, Password));
        stream.NewFrame += new NewFrameEventHandler(stream_NewFrame);
        stream.Start();

    }


    private void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Image img = eventArgs.Frame;
        byte[] b = GetImageBytes(eventArgs.Frame);
        HttpContext.Current.Response.OutputStream.Write(b, 0, b.Length);

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

为什么不将HttpContext保存在某处,以便可以从stream_NewFrame方法访问?我建议你在班上使用一个成员变量。

如果您想要将其封装得更多,请创建一个单独的类,将HttpContext提供给,并将stream_NewFrame方法放入该类中。类似的东西:

class Processor

      private HttpContext _context;

      public Processor(HttpContext context) {
           _context = context;
      }

      public void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
      {
          Image img = eventArgs.Frame;
          byte[] b = GetImageBytes(eventArgs.Frame);
          HttpContext.Current.Response.OutputStream.Write(b, 0, b.Length);
      }
}

然后在你的ProcessRequest中,你会这样:

public class ImageHandler:IHttpHandler,IRequiresSessionState {

public void ProcessRequest(HttpContext context)
{

    //Get parameter
    string Url = context.Request.QueryString["url"];
    string Username = context.Request.QueryString["username"];
    string Password = context.Request.QueryString["password"];

    //Set cache 
    HttpResponse Response = HttpContext.Current.Response;
    Response.Expires = 0;
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "multipart/x-mixed-replace";

    // create processor
    Processor p = new Processor(context);    

    // create MJPEG video source
    MJPEGStream stream = new MJPEGStream(string.Format("{0}/video.cgi?user={1}&pwd={2}", Url, Username, Password));
    stream.NewFrame += new NewFrameEventHandler(p.stream_NewFrame);
    stream.Start();

}

答案 1 :(得分:0)

MJPEGStream创建一个操作的后台线程。 HTTPContext.Current是一个线程局部变量,这意味着后台线程(它是调用stream_newFrame回调函数的线程)位于不同的线程上,因此它没有相同的{{1 (实际上它没有)。你需要以其他方式提供它。创建一个处理器对象以保存像Erik建议的引用的想法应该很好。