相关概念:
SAO,CAO
SingleTon(默认),SingleCall
用Spring.net 配置发布Remoting,服务器对象不需要继承MarshByRefObject,而是一个原生的PONO(plain .net object)。spring.net在运行时创建一个proxy继承MarshByRefObject.
<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" />
<!--<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao-aop.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall-aop.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton-aop.xml" />-->
</context>
<objects xmlns="http://www.springframework.net"
xmlns:r="http://www.springframework.net/remoting">
<description>Definitions of objects to be exported.</description>
<r:configurer filename="TestSpringService.ServerApp.exe.config" />
<!--singleton="false" 即为SAO的SingleCall,默认为Singleton-->
<object id="singletonCalculator" type="TestSpringService.Common.Calculator, TestSpringService.Common"
singleton="false"></object>
<!--导出接口-->
<r:saoExporter
targetName="singletonCalculator"
serviceName="RemotedSaoSingletonCalculatorWucg" />
<!--
Old fashion way without remoting config parser... -->
<object name="saoSingletonCalculator" type="Spring.Remoting.SaoExporter, Spring.Services">
<property name="TargetName" value="singletonCalculator" />
<property name="ServiceName" value="RemotedSaoSingletonCalculatorWucgOld" />
</object>
</objects>
</spring>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="8080" />
<!--<channel ref="http" port="8080" />-->
</channels>
</application>
</system.runtime.remoting>
</configuration>
Server端程序:
static void Main(string[] args)
{
try
{
// Force Spring to load configuration
ContextRegistry.GetContext();
Console.Out.WriteLine("Server listening...");
}
catch (Exception e)
{
Console.Out.WriteLine(e);
}
finally
{
Console.Out.WriteLine("--- Press <return> to quit ---");
Console.ReadLine();
}
}
<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" />
<!--<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/cao-aop.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleCall-aop.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton.xml" />
<resource uri="assembly://Spring.Calculator.RemoteApp/Spring.Calculator.RemoteApp.Config/saoSingleton-aop.xml" />-->
</context>
<objects xmlns="http://www.springframework.net"
xmlns:r="http://www.springframework.net/remoting">
<description>配置服务</description>
<!--注:serviceInterface一定为接口-->
<r:saoFactory id="calculatorService"
serviceInterface="TestSpringService.Common.ICalculator, TestSpringService.Common"
serviceUrl="http://localhost:8080/RemotedSaoSingletonCalculatorWucg"/>
<!--Old fashion way without remoting config parser...-->
<object id="calculatorServiceOld" type="Spring.Remoting.SaoFactoryObject, Spring.Services">
<property name="ServiceInterface" value="TestSpringService.Common.ICalculator, TestSpringService.Common" />
<property name="ServiceUrl" value="tcp://localhost:8080/RemotedSaoSingletonCalculatorWucgOld" />
</object>
</objects>
</spring>
</configuration>
Client程序:
static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
Console.WriteLine("Get Calculator...");
ICalculator firstCalc = (ICalculator)ctx.GetObject("calculatorServiceOld");
Console.WriteLine("Divide(11, 2) : " + firstCalc.Divide(11, 2));
Console.WriteLine("Divide(11, 2) : " + firstCalc.Add(11, 2));
Console.WriteLine("Get Calculator...");
ICalculator secondCalc = (ICalculator)ctx.GetObject("calculatorServiceOld");
Console.WriteLine("Divide(20, 3) : " + secondCalc.Divide(20, 3));
Console.ReadLine();
}
PS:
CAO导出
lazy-init="true" ></object>
<!--CAO导出接口 old fashion-->
<!--<object id="caoCalculatorOld" type="Spring.Remoting.CaoExporter, Spring.Services">
<property name="TargetName" value="caoCalculator" />
<property name="Infinite" value="false" />
<property name="InitialLeaseTime" value="2m" />
<property name="RenewOnCallTime" value="1m" />
</object>-->
<!--最新导出方式-->
<r:caoExporter targetName="caoCalculator" infinite="false">
<r:lifeTime initialLeaseTime="2m" renewOnCallTime="1m" />
</r:caoExporter>
<object id="caoCalculatorServiceOld" type="Spring.Remoting.CaoFactoryObject, Spring.Services">
<property name="RemoteTargetName" value="caoCalculator" />
<property name="ServiceUrl" value="tcp://localhost:8080" />
</object>
<r:caoFactory id="caoCalculatorService"
remoteTargetName="caoCalculator"
serviceUrl="tcp://localhost:8080" />
cs代码:
ICalculator caoCalc = (ICalculator)ctx.GetObject("caoCalculatorServiceOld");
Console.WriteLine("cao multiply:"+ caoCalc.Multiply(10, 20));
ICalculator caoNewCalc = (ICalculator)ctx.GetObject("caoCalculatorService");
Console.WriteLine("cao multiply:" + caoNewCalc.Add(10, 20));
Gets or sets the amount of time by which a call to the remote object renews the CurrentLeaseTime.
Assembly: mscorlib (in mscorlib.dll)
The ILease type exposes the following members.
Name | Description | |
---|---|---|
CurrentLeaseTime | Gets the amount of time remaining on the lease. | |
CurrentState | Gets the current LeaseState of the lease. | |
InitialLeaseTime | Gets or sets the initial time for the lease. | |
RenewOnCallTime | Gets or sets the amount of time by which a call to the remote object renews the CurrentLeaseTime. | |
SponsorshipTimeout | Gets or sets the amount of time to wait for a sponsor to return with a lease renewal time. |