自己动手写Remoting(三)

来源:互联网 发布:怎么把不安全网络ipad 编辑:程序博客网 时间:2024/05/19 23:24

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;


namespace Client
{
 class Client
 {
  static void Main(string[] Args)
  {
   #region TCP
   TcpChannel tcpChannel = new TcpChannel();  //因为客户端不需要CallBack端口
   ChannelServices.RegisterChannel(tcpChannel, false);
   #endregion

   
   HttpClientChannel httpChannel = new HttpClientChannel();
   ChannelServices.RegisterChannel(httpChannel, false);

   //取得远程的Remoting对象
   ShareDLL.InterMyService Service = (ShareDLL.InterMyService)Activator.GetObject(typeof(ShareDLL.InterMyService),
                                       "tcp://10.0.1.52:9000/ServiceTestTCP");

   ShareDLL.ICAOObject obj1 = Service.CreateCAO("Brian");       //在客户端映射创建一个对象
   Console.WriteLine(obj1.SayHelloCAO()); //可以直接使用这个对象


   ShareDLL.InterMyService ServiceHTTP = (ShareDLL.InterMyService)Activator.GetObject(typeof(ShareDLL.InterMyService),
                                       "http://10.0.1.52:9012/ServiceTestHTTP");
   ShareDLL.ICAOObject obj2 = ServiceHTTP.CreateCAO("KaiWei");

   Console.WriteLine(obj2.SayHelloCAO());

   Console.WriteLine(ServiceHTTP.SayHello("KaiWei"));
   
   Console.ReadLine();
  }

  
 }
}

以上是client改写成Http通道

中间层不变同上

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;

namespace Server
{
 class Server
 {
  static void Main(string[] args)
  {
   #region 走TCP通道
   TcpChannel tcpChannel = new TcpChannel(9000);
   //把tcpChannel对象注册到ChannelServices中
   ChannelServices.RegisterChannel(tcpChannel, false);
   //注册一个Remoting对象ServiceImpl(对象类型,对象名称,创建类型)
   RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceImpl), "ServiceTestTCP", WellKnownObjectMode.Singleton);
   #endregion

   
   HttpServerChannel httpChannel = new HttpServerChannel(9012);
   ChannelServices.RegisterChannel(httpChannel, false);
   RemotingConfiguration.ApplicationName = "Server";
   RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceImpl), "ServiceTestHTTP", WellKnownObjectMode.Singleton);
   RemotingConfiguration.RegisterActivatedServiceType(typeof(ShareDLL.InterMyService));

 


   Console.WriteLine("Server is Running");
   Console.ReadLine();
   Console.ReadLine();
  }
 }

 //建立一个继承MarshalByRefObject和自己定义接口的Class
 public class ServiceImpl : MarshalByRefObject, ShareDLL.InterMyService
 {
  
  
  public string SayHello(string AName)
  {
   return string.Format("Hello {0}", AName);
  }

  public ShareDLL.ICAOObject CreateCAO(string AName)
  {
   return new ICAOImpl(AName);
  }
 
 }
 //(Client Activate Object) CAO 创建一个客户端激活的对象类

 public class ICAOImpl : MarshalByRefObject, ShareDLL.ICAOObject
 {
  private string _Name;

  public string Name
  {
   get { return _Name; }
   set { _Name = value; }
  }

  public string SayHelloCAO()
  {
   return string.Format("Hello {0}, I'm CAO ", _Name);
  }

  public ICAOImpl(string AName)
  {
   _Name = AName;
  }
 }
}
以上remoting 内容改写为走HTTP通道!

 

原创粉丝点击