您的位置:首页 > 其它

我所理解的Remoting(3):创建CAO Service Factory使接口和实现相互分离

2007-04-02 19:04 826 查看
我们知道对于Remoting,有两种不同的Activation模式:Server Activation和Client Activation。他我在前面的系列文章中分析、比较了这两种不同激活方式的区别:Marshaling方式,远程对象创建的时机,状态的保持,生命周期的管理。 在编程模式方面Server Activation和Client Activation也具有一定的差异:为一个SAO(server activated object)和一个CAO(client activated object)注册一个远程对象类型的方式是不同的(Wellknown Service Type Re V.S. Activated Type Registration);为为一个SAO(server activated object)和一个CAO(client activated object)创建Proxy的方式也不一样:对于SAO,一般通过Activator的静态方法GetObject(传入一个远程对象的地址);而我们一般通过new 关键字或者Activator的静态方法CreateInstance。

String remoteAddress = "http://localhost/Artech.CAOFactory/CounterFactory.rem";        

ICounter counter = ICounterFactory counterFactory = (ICounterFactory)Activator.GetObject(typeof(ICounterFactory), remoteAddress);

对于Client Activation,由于我们在创建Proxy对象的时候,必须利用远程对象对应的原数据,所以在Client端,需要引用远程的对象所对应的dll。比如我们现在做一个简单的计数器的例子(Client远程调用获得计数器当前的计数)我们把业务逻辑封装在Counter Service的实体中。下图反映了这样一种架构的依赖关系。

using System; using System.Collections.Generic; using System.Text; namespace Artech.CAOFactory.ContractICounterFactory

using System;

using System.Collections.Generic;

using System.Text;

namespace Artech.CAOFactory.Contract

Step 3 实现Contract:Artech.CAOFactory.Service

using System;

using System.Collections.Generic;

using System.Text;

using Artech.CAOFactory.Contract;

namespace Artech.CAOFactory.Service

CounterFactoryService

using System;

using System.Collections.Generic;

using System.Text;

using Artech.CAOFactory.Contract;

namespace Artech.CAOFactory.Service

Step 3 通过IIS Host CounterFactoryService

修改编译配置把Dll生成在Project根目录的bin目录下,基于这个根目录创建虚拟目录(假设Alias就是Artech.CAOFactory),并添加Web.config。

<?xml version="1.0"?>

<configuration>

    <configSections>

        <section name="WorkflowRuntime" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

    </configSections>

    <WorkflowRuntime Name="WorkflowServiceContainer">

        <Services>

            <add type="System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

            <add type="System.Workflow.Runtime.Hosting.DefaultWorkflowCommitWorkBatchService, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

        </Services>

    </WorkflowRuntime>

    <appSettings/>

    <connectionStrings/>

    <system.web>        

        <compilation debug="false"/>        

        <authentication mode="Windows"/>        

    </system.web>

    <system.runtime.remoting>

        <application>

            <service>

                <wellknown type="Artech.CAOFactory.Service.CounterFactoryService, Artech.CAOFactory.Service"

                           mode ="SingleCall" objectUri="CounterFactory.rem"></wellknown>

            </service>

        </application>

    </system.runtime.remoting>

</configuration>

Step 4 创建客户端Artech.CAOFactory.Client

Program

using System;

using System.Collections.Generic;

using System.Text;

using Artech.CAOFactory.Contract;

using System.Threading;

using System.Runtime.Remoting;

namespace Artech.CAOFactory.Client

从上面的代码我们可以看到,我们希望使用的远程对象的Proxy(counter),是通过另一个SongleCall Proxy(counterFactory)获得的。

下面来运行,从输出结果来看,和我们平常使用的SAO方式的结果没有什么两样——同一个Proxy之间的调用状态被保留。



相关章节:

[原创]我所理解的Remoting(1):Marshaling & Activation - Part I

[原创]我所理解的Remoting(1):Marshaling & Activation - Part II

[原创]我所理解的Remoting(2):远程对象生命周期的管理—Part I

[原创]我所理解的Remoting (2) :远程对象的生命周期管理-Part II

[原创]我所理解的Remoting(3):创建CAO Service Factory使接口和实现相互分离

[原创].NET Remoting: 如何通过Remoting实现双向通信(Bidirectional Communication)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐