每次触发新事件时,我想将服务器发送的事件发送回客户端。
但是在回调中,我调用了一个名为commonEventBuffer.sendIncommingEvent()的静态方法,并传递了消息的文本(消息的文本也应作为响应对象发送回客户端),但是在该方法中,我得到了_resp.write()调用时出现SystenNullReferenceException。
作为一个期望消息,我得到“对象引用未设置为对象的实例。==> System.NullReferenceException
那我怎么办?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HomeScadaWebApp
{
/// <summary>
/// Summary description for incommingEventHandler
/// </summary>
public class incommingEventHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
commonEventBuffer._storedResponse = context.Response;
getNotification.myHandler += CB;
}
private void CB(object sender, EventArgs e)
{
string s = commonAESStuff.getEventChanges();
commonEventBuffer.sendIncommingEvent(s,commonEventBuffer._storedResponse);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
snippet from class commonEventBuffer.cs
public static void sendIncommingEvent(string s,HttpResponse _resp)
{
if (_resp != null)
{
try
{
_resp.ContentType = "text/event-stream";
_resp.Write("data:" + s.ToString() + "\n\n");
_resp.Flush();
}
catch (Exception ex)
{
}
}
}