之前用了Remoting技术实现了不同服务器之间的数据交互,最近闲下来然后整理了一下。
一、Remoting开发分为三个部分:远程对象、远程对象的宿主程序、客户端。
1、远程对象必须继承自MarshalByRefObject
2、远程对象需要一个运行的宿主程序,注册此对象服务
3、客户端需要引用远程对象的dll
二、服务端
TcpServerChannel channel = new TcpServerChannel(10099);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
"RemoteObject", WellKnownObjectMode.SingleCall);
System.Console.ReadLine();
三、客户端
1、将远程对象引用到客户端工程,
2、// 注册信道 http/tcp/icp(本地),多信道时名字不能相同
IDictionary httpProp = new Hashtable();
httpProp["name"] = "111";
httpProp["port"] = 0;
channel = new HttpChannel(httpProp,
new SoapClientFormatterSinkProvider(),
new SoapServerFormatterSinkProvider());
ChannelServices.RegisterChannel(channel, false);
#endregion
lsobj = (IServerObject)Activator.GetObject(typeof(IServerObject), "http://192.168.1.174:10099/TestRemoting");//IP地址需替换
returnvalue = lsobj.LetMeDo(connand).ToString();//接收从远程对象返回的结果信息