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

发布于 2024-11-29 13:21:53 字数 1466 浏览 1 评论 0原文

我已经极大地改变了这个问题,但事情就这样了。我正在从 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;
        }
    }

}

i've changed this question dramaticlly but here it goes. I'm reading a mjpeg stream from an IP cam which all goes fine. But now on every frame which I receive (See stream_NewFrame) I want to push this image to the client. But I cannot seem to figure out how to access the HttpContext.Current cause it always null inside that function. Does anyone know how to access the HttpContext context just like I can do inside the ProcessRequest function? I'm missing something obvious here I guess but I can't figure out what! Thank you for your time.

  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;
        }
    }

}

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

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

发布评论

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

评论(2

人事已非 2024-12-06 13:21:53

为什么不将 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();

}

Why don't you just save the HttpContext away sometwhere so it's accessible from the stream_NewFrame method? I would suggest using a member variable in your class.

If you'd like to encapsulate it more than that, create a separate class that you feed the HttpContext into, and put the stream_NewFrame method in that class instead. Something like:

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

and then in your ProcessRequest, you do like this:

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

}
树深时见影 2024-12-06 13:21:53

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

MJPEGStream creates a background thread that it operates on. HTTPContext.Current is a thread-local variable, which means that the background thread (which is the thread that is calling the stream_newFrame callback function) is on a different thread, so it doesn't have the same HTTPContext (in fact it has none). You'll need to provide it some other way. The idea of creating a Processor object to hold the reference like Erik suggested should work well.

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