Remoting技术是在不同应用程序域间互相通信的技术之一。其应用要包括四部分:
1. 需要被调用的应用接口
2. 实现上面应用接口的类
3. 服务器端模块: 定义信道并监听,注册远程对象
4. 客户端模块:定义信道,根据URL获取远程对象代理,使用代理调用远程服务器端的远程对象
注:
1. 客户端和服务器端都要添加下面引用
1. 需要被调用的应用接口
public interface IApp { string func(string sInput); }
2. 实现上面应用接口的类
public class RemoteApp: MarshalByRefObject, IApp { public string func(string sInput) { //操作 } }
3. 服务器端模块: 定义信道并监听,注册远程对象
using RemoteApp; TcpChannel channel = new TcpChannel(1234); ChannelServices.RegisterChannel(channel,false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteApp), "RemoteApplication", WellKnownObjectMode.SingleCall); System.Console.WriteLine("Press Enter key to exit"); System.Console.ReadLine();
4. 客户端模块:定义信道,根据URL获取远程对象代理,使用代理调用远程服务器端的远程对象
using IApp; TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel,false); ITax obj = (IApp)Activator.GetObject(typeof(IApp), "tcp://localhost:1234/RemoteApplication"); if (obj == null) { Console.WriteLine("Could not locate TCP server"); } String sInput = Console.ReadLine(); Console.WriteLine("结果为:" + obj.func(sInput).ToString()); Console.ReadLine();
注:
1. 客户端和服务器端都要添加下面引用
using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp;2. IApp与RemoteApp需要定义成2个类库