C# ashx处理程序,访问HttpContext.Current inside void

C# ashx处理程序,访问HttpContext.Current inside void,c#,stream,ashx,mjpeg,aforge,C#,Stream,Ashx,Mjpeg,Aforge,我戏剧性地改变了这个问题,但问题来了。我正在从IP摄像头读取mjpeg流,一切正常。但是现在在我收到的每一帧上(参见stream_NewFrame),我都想把这个图像推送到客户端。但是我似乎不知道如何访问HttpContext.Current,因为它在该函数中总是空的。是否有人知道如何访问HttpContext上下文,就像我在ProcessRequest函数中所做的那样?我想我遗漏了一些明显的东西,但我不知道是什么!谢谢你抽出时间 public class ImageHandler : IH

我戏剧性地改变了这个问题,但问题来了。我正在从IP摄像头读取mjpeg流,一切正常。但是现在在我收到的每一帧上(参见stream_NewFrame),我都想把这个图像推送到客户端。但是我似乎不知道如何访问HttpContext.Current,因为它在该函数中总是空的。是否有人知道如何访问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;
        }
    }

}

为什么不把
HttpContext
保存到别处,这样就可以通过
stream\u NewFrame
方法访问它呢?我建议在您的类中使用成员变量

如果您想对其进行更多的封装,请创建一个单独的类,将
HttpContext
提供给该类,并将
stream\u 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
中,您可以这样做:

公共类ImageHandler:IHttpHandler,iRequiresessionState {


MJPEGStream创建一个在其上运行的后台线程。HTTPContext.Current是一个线程局部变量,这意味着后台线程(即调用
stream\u newFrame
回调函数的线程)位于不同的线程上,因此它没有相同的
HTTPContext
(实际上它没有)。您需要以其他方式提供。像Erik建议的那样创建一个Processor对象来保存引用的想法应该可以很好地工作

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();

}