Web服务器调用MagicServer
执行某些计算。计算完成后MagicServer
返回最终结果字符串但在进行中MagicServer
通过事件发送消息。事件正在使用ClientEventSink
在Web Server中处理。我可以在ClientEventSink
中收到邮件,但由于它是Session
,因此无法设置null
。
app_code中的CS文件:MagicPerformer.cs
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Web;
using MagicAgent;
namespace MyWeb.Magic
{
class ClientEventSink : MessageEventSink
{
protected override void ClientCallback(string str)
{
HttpContext.Current.Session["MagicServerMessage"] = str; // **Could not get SESSION here. Current null.**
}
}
public class MagicPerformer
{
public string GetMagicString(int magicCount)
{
string magicString = string.Empty;
IChannel preregistered = ChannelServices.GetChannel("magicSink");
if (preregistered == null)
{
IDictionary props = new Hashtable();
props["name"] = "magicSink";
props["port"] = 0;
props["rejectRemoteRequests"] = "false";
BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
TcpChannel tcpCh = new TcpChannel(props, clientProv, serverProv);
ChannelServices.RegisterChannel(tcpCh, false);
}
MagicSearcher magician = (MagicSearcher)Activator.GetObject(typeof(MagicSearcher), "tcp://192.168.1.15:8010/magicsearcher");
if (magician == null)
throw new Exception("SERVER_NOT_RUNNING");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ClientEventSink),"RemotingBroadcast",WellKnownObjectMode.SingleCall);
ClientEventSink sink = new ClientEventSink();
ClientEventHandler clientEventHandler = new ClientEventHandler(sink.ServiceDispatchMethod);
try
{
magician.ClientServiceEvent += clientEventHandler;
magicString = magician.GetMagicStringFromServer(magicCount);
}
catch (System.Net.Sockets.SocketException se)
{
throw new Exception("CONNECTION_ERROR", se);
}
catch (Exception e)
{
}
magician = null;
return magicString;
}
}
}
如何从Session
设置ClientEventSink
,以便我可以在网页中显示进度消息。