ashx 处理程序,访问 void 内的 HttpContext.Current
我已经极大地改变了这个问题,但事情就这样了。我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将
HttpContext
保存在某个地方,以便可以通过stream_NewFrame
方法访问它?我建议在你的类中使用成员变量。如果您想对其进行更多封装,请创建一个单独的类,将
HttpContext
提供给其中,然后将stream_NewFrame
方法放入该类中。像这样:然后在您的 ProcessRequest 中,您可以这样做:
public class ImageHandler : IHttpHandler, IRequiresSessionState
{
Why don't you just save the
HttpContext
away sometwhere so it's accessible from thestream_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 thestream_NewFrame
method in that class instead. Something like:and then in your
ProcessRequest
, you do like this:public class ImageHandler : IHttpHandler, IRequiresSessionState
{
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 sameHTTPContext
(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.