Spring.NET学习笔记20——整合Remoting(应用篇) Level 200

  Spring.Services命名空间的目的是为业务服务提供位置的透明性。我们相信使用普通的接口和.NET类,用户应该可以用最简单的方式实现服务。我们也认为在将某个服务发布给客户端的时,应该只关心服务的配置,而无需关心服务的实现。在Spring.Services命名空间的支持下,可以用IoC容器中的服务导出对象将任一个普通对象发布为web服务、企业服务组件或远程对象。这里说的“普通对象”是指不继承或应用基础框架中任何特殊的基类(如MarshalByRefObject)或特性(如WebMethod)的对象。目前Spring.NET要求要发布的对象必须实现一个业务接口(按:即要发布的类必须实现某个接口,但这并非是限制,毕竟面向接口无论在什么时候都是好的编程实践)。Spring.NETRemoting导出类会自动为其创建一个继承自MarshalByRefObject代理类。在服务端,可以将SAO类型注册为SingleCallSingleton模式,也可以为每个对象配置生存期和租赁时间。另外,还可以将应用了AOP通知的对象发布为SAO。在客户端,可以用面向接口的最佳编程方式获取CAO的代理对象。Spring.NET还为Remoting创建了专门的schema来简化xml配置,不过我们仍然可以使用标准的schema来创建对象定义。

  传统方式,在服务器需要使用RemotingConfiguration.Configure("xxx.exe.config")的方式启动服务,在客户端需要IContract contract = (IContractActivator.GetObject ( typeof (IContract ), "tcp://localhost:1800/xxxx")的方式来注册服务。现在,一切都由Spring.NET而随之改变。

 

  准备条件:  

    [Serializable]
    
public   class  Person
    {
        
public   string  Name {  get set ; }
        
public   int  Age {  get set ; }
    }

 

     public   interface  IPersonContract
    {
        
void  SavePerson(Person model);

        Person GetPerson();
    }

 

 


    public class PersonContract : IPersonContract
    {

        
private Person m_person;

        
public void SavePerson(Person model)
        {
            
this.m_person = model;
            
this.m_person.Name += "";
            
this.m_person.Age++;
        }

        
public Person GetPerson()
        {
            
return this.m_person;
        }
    }

 

 

  一、部署为Singleton模式:

   服务端

 


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  
<configSections>
    
<sectionGroup name="spring">
      
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
      
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
    
</sectionGroup>
  
</configSections>


  
<spring>

    
<parsers>
      
<parser type="Spring.Remoting.Config.RemotingNamespaceParser, Spring.Services" />
    
</parsers>

    
<context>
      
<resource uri="config://spring/objects" />
    
</context>

    
<objects xmlns="http://www.springframework.net"
                     xmlns:r
="http://www.springframework.net/remoting">

      
<object id="personContract" type="RemotingServer.PersonContract, RemotingServer"/>

      
<!--自动配置服务-->
      
<r:configurer filename="RemotingServer.exe.config" />

      
<!--目标对象名和服务名-->
      
<r:saoExporter targetName="personContract"
                 serviceName
="PersonRemotingServer" />

    
</objects>

  
</spring>

  
<system.runtime.remoting>
    
<application>
      
<channels>
        
<channel ref="tcp" port="1800" />
      
</channels>
    
</application>
  
</system.runtime.remoting>
  
</configuration>

 


    class Program
    {
        
static void Main(string[] args)
        {
            IApplicationContext ctx 
= ContextRegistry.GetContext();

            Console.WriteLine(
"服务器已启动");

            Console.ReadLine(); 
        }
    }

 

  

  客户端


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  
<configSections>
    
<sectionGroup name="spring">
      
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    
</sectionGroup>
  
</configSections>


  
<spring>

    
<context>
      
<resource uri="config://spring/objects" />
    
</context>

    
<objects xmlns="http://www.springframework.net">

      
<object id="PersonRemotingServer" type="Spring.Remoting.SaoFactoryObject, Spring.Services">
        
<property name="ServiceInterface" value="IContract.IPersonContract, IContract" />
        
<property name="ServiceUrl" value="tcp://localhost:1800/PersonRemotingServer" />
      
</object>

    
</objects>

  
</spring>
  
</configuration>

 


    class Program
    {
        
static void Main(string[] args)
        {
            IApplicationContext ctx 
= ContextRegistry.GetContext();

            IPersonContract server 
= (IPersonContract)ctx.GetObject("PersonRemotingServer");

            Person tempPerson 
= server.GetPerson();
            tempPerson 
= tempPerson ?? new Person() { Name = "刘冬", Age = 26 };
            server.SavePerson(tempPerson);

            Person person 
= server.GetPerson();
            
string msg = string.Format("姓名:{0},年龄:{1}", person.Name, person.Age);
            Console.WriteLine(msg);

            Console.ReadLine(); 
        }
    }

 

  运行效果:

 

  第一次调用

 

  第二次调用

 

  二、部署为SingleCall模式:

  修改对象的属性

< object  id ="personContract"  type ="RemotingServer.PersonContract, RemotingServer"  singleton ="false" />

  


    class Program
    {
        
static void Main(string[] args)
        {
            IApplicationContext ctx 
= ContextRegistry.GetContext();

            IPersonContract server 
= (IPersonContract)ctx.GetObject("PersonRemotingServer");

            Person tempPerson 
= server.GetPerson();
            tempPerson 
= tempPerson ?? new Person() { Name = "刘冬", Age = 26 };
            server.SavePerson(tempPerson);

            Person person 
= server.GetPerson();
            
string msg = person == null ? "对象为空" : string.Format("姓名:{0},年龄:{1}", person.Name, person.Age);
            Console.WriteLine(msg);

            Console.ReadLine(); 
        }
    }

 

 

  调用

 

  代码下载

 

 

  返回目录

作者:刘冬.NET 博客地址:http://www.cnblogs.com/GoodHelper/ 欢迎转载,但须保留版权
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值