Spring.NET教程(二十)整合Remoting(应用篇)

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

传统方式,在服务器需要使用RemotingConfiguration.Configure("xxx.exe.config")的方式启动服务,在客户端需要IContract contract = (IContract) Activator.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();

}

PersonContract

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模式:

服务端

App.config

<?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>

Program

class Program

{

static void Main(string[] args)

{

IApplicationContext ctx = ContextReGIStry.GetContext();

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

Console.ReadLine(); 

}

}

客户端

App.config

<?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>

Program

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"/>

Program

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(); 

}

}

调用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzhdim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值