[原创] 分布式系统框架搭建 PartOne-.net Remoting

Remoing 例程
1. 创建 Remoting Server (RemotingSrvConsole) Client (RemotingClient) 、以及公用函数库 (RemotingServerLib) 项目。
a)RemotingServerLib 项目
添加类RemoteObject 继承 MarshalByRefObject添加代码
public RemoteObject()
        {
            Console.WriteLine("Constructor called");
        }
        public string Hello(string name)
        {
            Console.WriteLine(name);
            return "Hello " + name;
        }
        public DataSet GetDataSet(String SQLStr)
        {
            DataSet ReturnDs=new DataSet();
            String ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb;";
            using (OleDbDataAdapter OleAp = new OleDbDataAdapter(SQLStr, ConnStr))
            {
                OleAp.Fill(ReturnDs);
                Console.WriteLine("执行了SQL " + SQLStr);
                return ReturnDs;
            }
  }
b)RemotingSrvConsole 项目
                         i. 添加 Northwind.mdb 数据库设置并复制到输入目录 .
                       ii. 添加 RemotingConfiguration.Configure 配置文件内容为
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application name="RemoteClassLibServer">
      <service>
        <wellknown mode="SingleCall" type="RemotingServerLib.RemoteObject,RemotingServerLib"
         objectUri="RemoteObject">
        </wellknown>
      </service>
      <channels>
        <channel ref="tcp server" port="9000"/>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>
                       iii. 添加对 RemotingSrvConsole 项目的引用
c)RemotingClient 项目
                         i. 添加对 .net System.Runtime.Remoting 的引用 , 以及 RemotingServerLib 项目引用 . 合适的地方初始化 Remoting 通讯连接,我的窗体为
      



代码如下 :
public partial class RemotingClientFrm : Form
    {
        public RemotingClientFrm()
        {
            InitializeComponent();
            this.InitCommReote();
        }
        RemoteObject loader;
        void InitCommReote()
        {
            ChannelServices.RegisterChannel(new TcpClientChannel(),true);
            loader = (RemoteObject)Activator.GetObject(
            typeof(RemoteObject), "tcp://localhost:9000/RemoteObject");
            loader.Hello("Test Action");
        }
        private void button1_Click(object sender, EventArgs e)
        {
          this.dataGridView1.DataSource = loader.GetDataSet("Select * From Customers").Tables[0];
        }
    }
2. 新建两个解决方案 SoluRemotingServer 以及 SoluRemotingClient
其中 SoluRemotingServer 添加项目 RemotingSrvConsole 、以及 RemotingServerLib
其中 SoluRemotingServer 添加项目 RemotingClient 、以及 RemotingServerLib
(这个是为了方便调试)其中两解决方案中 RemotingServerLib 物理位置一致
如下图所示
RemotingSrvConsole 解决方案

RemotingClient 解决方案

启动 RemotingSrvConsole 再启动 RemotingClient

(RemotingSrvConsole)


(RemotingClient 点击了 button1)
此时 RemotingSrvConsole 已经有客户端 显示



OK! 我们的简单的 Remoting 构建成功了!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值