Remoting简单应用

Remoting技术是在不同应用程序域间互相通信的技术之一。其应用要包括四部分:
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个类库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值