今日学习 (使用.NET Remoting 建立分布式应用程序(一))(4月6日)

讲师:任旻

远程对象的两个含义
操作远程对象
Ø对象运行在远程,客户端向他发送消息。
ØMarshalByRefObject
传递远程对象
Ø将远程的对象拿到本地,或者将本地对象发送
过去。
Ø对副本进行操作
Ø[Serializable] 或ISerializable

谁来激活对象?
服务器激活(WellKnown)
ØSingleton
ØSingleCall
客户端激活

通道(Channels)
一个远程对象使用通道发送和接收消息
Ø服务器选择一个通道来监听请求(request)
Ø客户端选择通道来和服务器通讯
lRemoting提供了内置的通道
ØTCP通道和HTTP通道
Ø你也可以编写自己的通道

开发Remoting三步走
创建远程对象
创建一个应用程序作为“宿主(host)”,以接收客户请求
创建一个客户端调用远程对象

第一步:创建远程对象
l继承System.MarshalByRefObject
public class HelloServer : MarshalByRefObject
{
……
}

第二步:创建宿主应用程序
l注册通道
Ø内置通道:TCP,HTTP
l注册服务器激活的远程对象
(WellKnown)
ØSingleton,SingleCall
ØURL
l运行宿主程序

第三步:建立客户端程序
l注册通道
Ø内置通道:TCP,HTTP
l根据URL得到对象代理
l使用代理调用远程对象

传递参数
传递简单类型
Øint,doulbe,string,enum……
传递可序列化的类型
ØArrayList,Hashtable,DataSet……
传递自定义类型
Ø[Serializable]

Serialization VS.MarshalByRefObject
按值列集(Serialization)
Ø得到远程对象的副本。
Ø对副本的操作不影响远程对象。
Ø不论远程对象是Singleton还是SingleCall
按引用列集( MarshalByRefObject )
Ø得到远程对象引用,本地创建代理(Proxy)
Ø通过(Proxy)对远程对象访问。
ØSingleton记录更改,SingleCall无状态。

示例程序:

Server.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

namespace RemotingSamples
{

    public class Server
 {
  public static int Main(string [] args)
  {

   TcpChannel chan1 = new TcpChannel(8085);
   HttpChannel chan2 = new HttpChannel(8086);

   ChannelServices.RegisterChannel(chan1);
   ChannelServices.RegisterChannel(chan2);


            RemotingConfiguration.RegisterWellKnownServiceType
                (
                typeof(HelloServer),
                "SayHello",
                WellKnownObjectMode.Singleton
                );
   

   System.Console.WriteLine("Press Enter key to exit");
   System.Console.ReadLine();
   return 0;
  }

 }
}

HelloServer.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace RemotingSamples
{
    public class HelloServer : MarshalByRefObject
    {
        public HelloServer()
        {
            Console.WriteLine("HelloServer activated");
        }
        public String HelloMethod(String name)
        {
            Console.WriteLine(
                "Server Hello.HelloMethod : {0}", name);
            return "Hi there " + name;
        }
    }
}

Client.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.IO;

namespace RemotingSamples
{
 public class Client
 {
        public static void Main(string[] args)
        {
            //使用TCP通道得到远程对象
            TcpChannel chan1 = new TcpChannel();
            ChannelServices.RegisterChannel(chan1);
            HelloServer obj1 = (HelloServer)Activator.GetObject(
                typeof(RemotingSamples.HelloServer),
                "tcp://localhost:8085/SayHello");
            if (obj1 == null)
            {
                System.Console.WriteLine(
                    "Could not locate TCP server");
            }
            //使用HTTP通道得到远程对象
            HttpChannel chan2 = new HttpChannel();
            ChannelServices.RegisterChannel(chan2);
            HelloServer obj2 = (HelloServer)Activator.GetObject(
                typeof(RemotingSamples.HelloServer),
                "http://localhost:8086/SayHello");
            if (obj2 == null)
            {
                System.Console.WriteLine(
                    "Could not locate HTTP server");
            }

            Console.WriteLine(
                "Client1 TCP HelloMethod {0}",
                obj1.HelloMethod("Caveman1"));
            Console.WriteLine(
                "Client2 HTTP HelloMethod {0}",
                obj2.HelloMethod("Caveman2"));
            Console.ReadLine();
        }
/*
   RemotingConfiguration.Configure(@"C:/Documents and Settings/RenMin/桌面/Remoting/Demo/02-SimpleRemoting/Client/client.exe.config");
   HelloServer obj2 = new HelloServer();
*/
 }
}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值