zoukankan      html  css  js  c++  java
  • 再谈Remoting

    下面的Remoting程序,采用了Singleton模式的服务器端激活的方式,分为三部分。
    General:一个远程对象公用的程序集。
    Server:服务器端
    Client:客户端


    一 创建第一个Remoting控制台程序

    在创建程序之前,先把几个要用到的类先介绍一下:

    1) MarshalByRefObject类:

    允许在支持远程处理的应用程序中跨应用程序域边界访问对象。
    应用程序域是一个操作系统进程中一个或多个应用程序所驻留的分区。同一应用程序域中的对象直接通信。不同应用程序域中的对象的通信方式有两种:一种是跨应用程序域边界传输对象副本,一种是使用代理交换消息。
    MarshalByRefObject 是通过使用代理交换消息来跨应用程序域边界进行通信的对象的基类。不是从 MarshalByRefObject 继承的对象根据值隐式封送。当远程应用程序引用根据值封送的对象时,将跨应用程序域边界传递该对象的副本。
    MarshalByRefObject 对象在本地应用程序域的边界内可直接访问。远程应用程序域中的应用程序首次访问 MarshalByRefObject 时,会向该远程应用程序传递代理。对该代理后面的调用将封送回驻留在本地应用程序域中的对象。
    当跨应用程序域边界使用类型时,类型必须是从 MarshalByRefObject 继承的,而且由于对象的成员在创建它们的应用程序域之外无法使用,所以不得复制对象的状态。

    2) ChannelServices 类:

    提供帮助进行远程处理信道注册、解析和 URL 发现的静态方法。
    信道跨远程处理边界(例如 AppDomains、进程和计算机)在应用程序之间传输消息。这些交叉可以是入站和出站的。信道可以在终结点上侦听入站消息,向终结点发送出站消息,或同时进行这两种操作。这在运行库中提供一个可扩展性点,以便插入各种协议,即使运行库可能并不在信道的另一端。运行库对象可用于公开各种语义和实体。信道提供可扩展性点以将消息在特定协议间来回转换。

    3) TcpChannel 类:

    提供使用 TCP 协议传输消息的信道实现。

    一) Remoting程序的创建(分三步走)[附源代码下载].

    1 创建General远程对象

    此对象必须继承MarshalByRefObject。
    代码如下:

     1/// <summary>
     2    /// 远程对象类
     3    /// </summary>

     4    public class RemoteObject : MarshalByRefObject
     5    {
     6        public RemoteObject()
     7        {
     8            Console.WriteLine("I am from Remote Object!");
     9        }

    10        public string GetClassName(string name)
    11        {
    12            return name;
    13        }

    14    }


    2.创建Server作为"
    宿主(host)",以接收客户请求

    1)注册通道,下文注册了一个端口号为8000的端口。
       注册后服务器就会监听来自8000端口的请求。

    2)注册服务器激活的远程对象

     1/// <summary>
     2    /// 服务器端
     3    /// </summary>

     4    public class Server
     5    {
     6        static void Main(string[] args)
     7        {
     8            //实例化并注册一个TCP通道
     9            IChannel tcpchannel = new TcpChannel(8000);
    10            ChannelServices.RegisterChannel(tcpchannel, true);
    11
    12            //将服务端上的对象类型注册为已知类型
    13            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "GetObject", WellKnownObjectMode.Singleton);
    14
    15            Console.WriteLine("Press Enter Key to Exit!");
    16            Console.ReadLine();
    17      
    18        }

    19    }


    3.创建Client,调用远程对象

    1)注册通道
    2)根据URL得到对象代理
    3)使用代理调用远程对象

     1/// <summary>
     2    /// 客户端
     3    /// </summary>

     4    public class Client
     5    {
     6        static void Main(string[] args)
     7        {
     8            //实例化并注册一个TCP通道
     9            IChannel tcpChannel = new TcpChannel();
    10            ChannelServices.RegisterChannel(tcpChannel, true);
    11
    12            //激活远程对象
    13            RemoteObject obj = (RemoteObject)Activator.GetObject(typeof(RemotingDemo.RemoteObject),"tcp://localhost:8000/GetObject");
    14
    15            if (obj == null)
    16            {
    17                Console.WriteLine("Could not get Tcp Server");
    18            }

    19
    20            //调用远程方法
    21            Console.WriteLine("Client TCP GetClassName: {0}",obj.GetClassName("ring1981"));
    22
    23            Console.ReadLine();
    24        }

    25    }

    到此为止,Remoting应用程序建立完毕!

    运行结果:

    上面的例子是全部通过编程实现,在下篇文章里里我会介绍如何通过配置文件实现。

    二)关于运行Remoting程序的一点说明

    由于不少初学者说按"F5"看不到这样的结果,其实是没有学会如何运行这种多个项目,出现多个Dos窗口结果的工程。

    我就附带介绍一下如何运行上面这个例子吧:
    第一步: 选择"Debug"->"Start Debuging"或"Debug->Start without Debug"都行,点击后会出现一个Dos窗口显示结果。
    第二步: 选择"Soluting Explorer"下的"Client"项目,选中后点击鼠标右键,点击后出现一个菜单。
    第三步: 选择"Debug"->"Start new instance",单击就OK了。
     

    二 使用配置文件建立Remoting程序


    一) 服务器端配置(app.config)

     1<configuration>
     2  <system.runtime.remoting>
     3    <application>
     4      <channels>
     5        <channel ref="tcp" port="8000" />
     6      </channels>
     7      <service>
     8        <wellknown mode="Singleton"
     9                   type="RemotingDemo.RemoteObject, General"
    10                   objectUri="GetObject" />
    11      </service>
    12    </application>
    13  </system.runtime.remoting>
    14</configuration>
    15

    此时服务器端代码为:

     1 /// <summary>
     2    /// 服务器端
     3    /// </summary>

     4    public class Server
     5    {
     6        static void Main(string[] args)
     7        {            
     8            RemotingConfiguration.Configure("server.exe.config"true);
     9
    10            Console.WriteLine("Press Enter Key to Exit!");
    11            Console.ReadLine();      
    12        }

    13    }


    二) 客户端配置 (client.exe.config)

     1<configuration>
     2    <system.runtime.remoting>
     3        <application>
     4            <client>
     5                <wellknown type="RemoteObject, General" url="tcp://localhost:8000/GetObject" />
     6            </client>
     7            <channels>
     8                <channel ref="tcp" port="0"></channel>
     9            </channels>
    10        </application>
    11    </system.runtime.remoting>
    12</configuration>
    13

    此时客户端的代码为:

    /// <summary>
        
    /// 客户端
        
    /// </summary>

        public class Client
        
    {
            
    static void Main(string[] args)
            
    {
                
                RemotingConfiguration.Configure(
    @"client.exe.config"true);
                RemoteObject obj 
    = new RemoteObject();
                
    if (obj == null)
                
    {
                    Console.WriteLine(
    "Could not get Tcp Server");
                }


                
    //调用远程方法
                Console.WriteLine("Client TCP GetClassName: {0}",obj.GetClassName("ring1981"));

                Console.ReadLine();
            }

        }


    一个标准写全的Remoting Configuration文件应该是如下结构:

     1<configuration>
     2   <system.runtime.remoting>
     3      <application>
     4        <lifetime />
     5        <channels />
     6        <service />
     7        <client />
     8      </application>
     9   </system.runtime.remoting>
    10</configuration>
    11
    12

    接下来结合配置文件介绍一下各结点的具体涉及内容:

    一) 生命周期(LifeTime):用于配置对象的生命周期.

    ATTRIBUTE

    DESCRIPTION

    leaseTime

    The initial time to live (TTL) for your objects (默认5 分钟)

    sponsorshipTimeout

    The time to wait for a sponsor's reply (默认2 分钟)

    renewOnCallTime

    The time to add to an object's TTL when a method is called (默认2 分钟)

    leaseManagerPollTime

    The interval in which your object's TTL will be checked (默认10 秒)


    例子:
    1<lifetime
    2   leaseTime="90MS"
    3   renewOnCallTime="90MS"
    4   leaseManagerPollTime="100MS"
    5/>

    这里MS是毫秒的意思.如果想配置成秒,那就用S;分钟是M;小时是H;天是D。


    二) 通道:

    如果在服务器端注册一个TCP通道,监听端口为1000,那么就该如下配置:

    1<channels>
    2   <channel ref="tcp" port="1000">
    3</channels>

    如果是在客户端,那么port必须为0。

    Channel结点包含如下内容:

    ATTRIBUTE

    DESCRIPTION

    ref

    引用先前定义好的通道("tcp"或"http"),或者引用先前在配置文件里定义好的通道

    displayName

    此属性只用于.NET Framework配置工具

    type

    该属性在ref没有定义时,是托管的。Attribute that is mandatory when ref has not been specified. Contains the exact type (namespace, classname, assembly) of the channel's implementation. When the assembly is in the GAC, you have to specify version, culture, and public key information as well.

    port

    端口号



    三) Service:

    Service属性允许你注册SAOs和CAOs.该结点下可以包含多和 <wellknown> 和<activated> 属性。结构示例如下:

     1<configuration>
     2  <system.runtime.remoting>
     3    <application>
     4      <service>
     5          <wellknown />
     6          <activated />
     7      </service>
     8    </application>
     9  </system.runtime.remoting>
    10</configuration>
    11
    12

     

    1) WellKnow:

    ATTRIBUTE

    DESCRIPTION

    type

    写法如"<namespace>.<classname>, <assembly>"。程序集中已发布类的类型。

    mode

    指出具体对象调用类型("Singleton" 或"SingleCall").

    objectUri

    用于呼叫对象的终端URI。当对象宿主在IIS中,URI需以.soap或.rem结尾。

    displayName

    用于.NET Framework Configuration Tool中。


    结构示例如下:
     1<configuration>
     2  <system.runtime.remoting>
     3    <application>
     4      <channels>
     5        <channel ref="http" port="1234" />
     6      </channels>
     7      <service>
     8        <wellknown mode="Singleton"
     9                   type="Server.CustomerManager, Server"
    10                   objectUri="CustomerManager.soap" />
    11      </service>
    12
    13    </application>
    14  </system.runtime.remoting>
    15</configuration>
    16


     2) Activated:

    ATTRIBUTE

    DESCRIPTION

    type

    写法如"<namespace>.<classname>, <assembly>"。程序集中已发布类的类型。


    结构示例如下:
     1<configuration>
     2   <system.runtime.remoting>
     3      <application>
     4         <channels>
     5            <channel ref="http" port="1234" />
     6         </channels>
     7         <service>
     8            <activated type="MyObject, MyAssembly"/>
     9         </service>
    10      </application>
    11   </system.runtime.remoting>
    12</configuration>
    13
    14

     

    四) Client:

    与<service>属性相对,结构与<service>非常类似。
    结构示例如下:

     1<configuration>
     2   <system.runtime.remoting>
     3      <application>
     4         <client>
     5            <wellknown />
     6            <activated />
     7        </client>
     8      </application>
     9   </system.runtime.remoting>
    10</configuration>
    11
    12


    三 Remoting的部署

    一) 如何将Remoting部署为Windows Service?

    1) 新建一个windows service项目
    2) 在Service1的OnStart()下写好方法,代码如下:

    1protected override void OnStart(string[] args)
    2  {
    3   RemotingConfiguration.Configure(@"C:\..\WindowsService1.exe.config");
    4  }

    3) 打开Visual Studio 2005 Command Prompt窗口.
    4) 进入WindowsService1工程的bin\debug目录.
         cd D:\.......\bin\debug
    5) 输入installutil WindowsService1.exe,回车! 安装完毕!此时在Windows服务中,就可以看到新注册的服务.
    6) 如果要卸载该服务,输入的命令为 installutil /u WindowsService1.exe.


    二) 如何将Remoting部署在IIS中?

    1) 将Remoting远程对象项目编译成程序集
    2) 创建虚拟目录和bin文件夹
    3) 将生成的程序集放在bin文件夹下.
    4) 编写一个web.config,内容和Server下的配置文件相似,但不需要<channel>结点.
    5) 将web.config放在虚拟目录下。
    完毕! 此时客户端就可以访问远程对象了。

  • 相关阅读:
    关于cookie的一点知识
    一点简单的关于ASP.NET下载
    一个小小小问题
    复习linq
    拾遗一些关于存储过程
    一般处理程序(下)
    复习一下递归
    复习JS和jQuery
    一点关于Ajax和一个等待图标的显示
    C#开发BIMFACE系列14 服务端API之批量获取转换状态详情
  • 原文地址:https://www.cnblogs.com/chorrysky/p/575136.html
Copyright © 2011-2022 走看看