.NET Remoting 最简单示例

学习技术知识一个好的方法是先动手,再深入,

给出一个最简单的Remoting程序示例(C#)如下:


Step1:创建类库(DLL)工程RemotingObjects,类Person代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RemotingObjects
{
    public interface IPerson
    {
        String getName(String name);

    }

    public class Person : MarshalByRefObject, IPerson
    {
        public Person()
        {
            Console.WriteLine("[Person]:Remoting Object 'Person' is activated.");
        }

        public String getName(String name)
        {
            return name;
        }
    }
}
Step2:创建控制台工程RemotingServer(添加项目引用RemotingObjects),类Server代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;


namespace RemotingServer
{
    class Server
    {
        static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObjects.Person), "RemotingPersonService", WellKnownObjectMode.SingleCall);

            System.Console.WriteLine("Server:Press Enter key to exit");
            System.Console.ReadLine();
        }
    }
}

Step3:创建控制台工程RemotingClient(添加项目引用RemotingObjects及必要类库),类Client代码如下:

(PS:正式应用开发,不需要也不应该直接引用RemotingObjects类库,而应该引用相关Remoting类的接口库。)

using RemotingObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace RemotingClient
{
    class Client
    {
        static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);
            IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "tcp://localhost:8080/RemotingPersonService");
            if (obj == null)
            {
                Console.WriteLine("Couldn't crate Remoting Object 'Person'.");
            }

            Console.WriteLine("Please enter your name:");
            String name = Console.ReadLine();
            try
            {
                Console.WriteLine(obj.getName(name));
            }
            catch (System.Net.Sockets.SocketException e) {
                Console.WriteLine(e.ToString());
            }
                Console.ReadLine();
        }
    }
}
Step4:运行编译出的EXE:RemotingServer.exe和RemotingClient.exe,查看运行结果。


於太阳宫

2014/01/21


  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值