Remoting实例(客户端发送信息)
这里为大家提供一个最简单的Remoting实例,应该没有太多多余的代码
本程序只是实现了客户端给服务端发送信息的最简单实例
服务端代码
HttpChannel channelClient = new HttpChannel(8081);
ChannelServices.RegisterChannel(channelClient,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObj), "GetMes", WellKnownObjectMode.Singleton);
RemotingObj.MesEvent += new MessageEvent(RemotingObj_MesEvent
ChannelServices.RegisterChannel(channelClient,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObj), "GetMes", WellKnownObjectMode.Singleton);
RemotingObj.MesEvent += new MessageEvent(RemotingObj_MesEvent
客户端代码
Obj = (IMessage)Activator.GetObject(typeof(IMessage), "http://localhost:8081/GetMes");
公共接口
public delegate void MessageEvent(string str);
public interface IMessage
{
void SendMes(string str);
}
public interface IMessage
{
void SendMes(string str);
}
公共类
public class RemotingObj:MarshalByRefObject,IMessage
{
public static event MessageEvent MesEvent;
public void SendMes(string str)
{
if (MesEvent != null)
{
MesEvent(str);
}
}
//重写远程对象的生命周期,使生命周期为无限
public override object InitializeLifetimeService()
{
return null;
}
}
{
public static event MessageEvent MesEvent;
public void SendMes(string str)
{
if (MesEvent != null)
{
MesEvent(str);
}
}
//重写远程对象的生命周期,使生命周期为无限
public override object InitializeLifetimeService()
{
return null;
}
}
下面是实例下载