.Net Remoting 对象生存周期(3)

MS .Net Remoting框架是完全可配置的,可由配置文件定制Remoting的各种行为,下面讨论下SAOs(服务器端可激活对象的配置)的配置文件,租约与CAOs(客户端可激活对象),在实际汲及Remoting的项目中,推荐使用配置文件,这样可以简化代码,随时更改,通道,端口,URL的设置而不用重新编译程序,接下来,我们在上一个Demo的基础上,改为配置文件实现一个简单的Remoting程序

首先,看一下SAOs配置文件:
 1<?xml version="1.0" encoding="utf-8" ?>
 2
 3<configuration>
 4  <system.runtime.remoting><!--所有关于Remoting的配置,可据此在MSDN中查到Remoting的所有配置节点-->
 5    <application>
 6      <service><!--注册一个服务-->
 7        <wellknown mode="Singleton" objectUrl="SsyHello" type="General.HelloServer,General"/><!--注册一个众所周知的对象,及其它各种信息-->
 8      </service>
 9      <channels><!--注册通道 -->
10        <channel port="8086" ref="http"/><!--ref指引用machine.config中的配置-->
11      </channels>
12    </application>
13  </system.runtime.remoting>
14</configuration>

此时在对于Demo1中的Server端代码如下:
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Runtime.Remoting.Channels;
 5using System.Runtime.Remoting;
 6using System.Runtime.Remoting.Channels.Tcp;
 7using System.Runtime.Remoting.Channels.Http;
 8using General;
 9namespace Server
10{
11   public class Server
12    {
13       /// <summary>
14       /// 第二步,创建服务器端
15       /// </summary>
16       /// <param name="args"></param>
17       /// <returns></returns>

18        static int Main(string[] args)
19        {
20            ////Remoting内置的两种通道,也可自定义自己的通道
21            //TcpChannel tcpChannel = new TcpChannel(8085);//tcp通道
22            //HttpChannel httpChannel = new HttpChannel(8086);//http通道
23
24
25            ////服务器端对通道进行注册,注册完后,服务器就对通道进行监听
26            //ChannelServices.RegisterChannel(tcpChannel,false);
27            //ChannelServices.RegisterChannel(httpChannel,false);
28
29            ////把远程对象注册到服务器上,注册众所周知的服务类型,WellKnown---都是服务器激活的对象
30            //RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloServer),"SayHello",WellKnownObjectMode.Singleton);
31            ///*
32            // * Singleton 服务器端只产生单例,任何请求共用,保存状态,
33            // * Singlecall 客户端一个请求对应一个实例,没有状态
34            // * 
35            // * 
36            // * 
37            // * 
38            //*/
39            string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//得到APP.confg文件在程序可执行目录下生成的server.exe.config文件的路径
40            RemotingConfiguration.Configure(path);
41            Console.Write("服务器开始监听,按任意銉退出");
42            Console.ReadLine();
43            return 0;
44        }

45    }

46}

47

从以上可以看出,通道的定义,注册及对象在服务器上的注册等环节均用配置文件实现了,同理对于客户端如下:
配置文件:
 1<?xml version="1.0" encoding="utf-8" ?>
 2<configuration>
 3  <system.runtime.remoting>
 4    <system.runtime.remoting>
 5      <application>
 6        <client>
 7          <wellknown url="http://localhost:8086/SayHello"  type="General.HelloServer,General"/>
 8        </client>
 9        <channels>
10          <channel port="0" ref="http"/>
11        </channels>
12      </application>
13    </system.runtime.remoting>
14  </system.runtime.remoting>
15</configuration>

客户端代码为:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using General;
namespace Client
{
    
class Client
    
{
        
/// <summary>
        
/// 第三步,创建客户端
        
/// </summary>
        
/// <param name="args"></param>

       public static void Main(string[] args)
        
{
            
////使用TCP通道得到远程对象代理,不用监听
            //TcpChannel tcpChannel1 = new TcpChannel();
            
//ChannelServices.RegisterChannel(tcpChannel1,false);
            
//HelloServer helloServer1 = (HelloServer)Activator.GetObject(typeof(General.HelloServer), "tcp://localhost:8085/SayHello");
            
//if (helloServer1 == null)
            
//{
            
//    Console.Write("Could not locate TCP server");
            
//}


            
////使用HTTP通道得到远程对象代理,不用监听
            //HttpChannel httpChannel2 = new HttpChannel();
            
//ChannelServices.RegisterChannel(httpChannel2, false);
            
//HelloServer helloServer2 = (HelloServer)Activator.GetObject(typeof(General.HelloServer), "http://localhost:8086/SayHello");
            
//if (helloServer2 == null)
            
//{
            
//    Console.Write("Could not locate HTTP server");
            
//}
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            HelloServer helloServer2 
= new HelloServer();
           
//一般方法调用
            
//Console.WriteLine("TCP HelloMethod {0}", helloServer1.HelloMethod("TcpChannel"));
            Console.WriteLine("HTTP HelloMethod {0}", helloServer2.HelloMethod("HTTPChannel"));

           
//传递自定义类型参数
           
// Console.WriteLine("TCP HelloMethod {0}", helloServer1.HelloUserMethod(new User("wumaosheng",false)));
            Console.WriteLine("http HelloMethod {0}", helloServer2.HelloUserMethod(new User("heqichang"true)));


            
//测试SingleTon和SingleCall,多次调用
            int count;
            
//Console.WriteLine("TCP HelloMethod {0} count:{1}", helloServer1.HellTonOrCall("mswu",out count),count);
            Console.WriteLine("http HelloMethod {0} count:{1}", helloServer2.HellTonOrCall("mswu"out count), count);
            Console.WriteLine(
"http HelloMethod {0} count:{1}", helloServer2.HellTonOrCall("mswu"out count), count);

            Console.ReadLine();
        }

    }

}


远程对象及其它部分均不需改变,未完,待续....

原文地址:https://www.cnblogs.com/mshwu/p/772708.html