spring.net-----(4)

五、使用Remoting对原有系统进行改造

如果使用Remoting技术对HelloGenerator进行改造,使其具有分布式远程访问能力,那么在不使用Ioc技术的情况下,我们将会作出如下调整:

(1)让HelloGenerator继承自MarshalByRefObject类

如果要让某个对象具有分布式的功能,必须使其继承自MarshalByRefObject,这样才可以具有远程访问的能力。因此我们需要调整EnHelloGenerator和CnHelloGenerator的代码。这里以EnHelloGenerator为例:

using System;
namespace IocInCSharp
{
public class EnHelloGenerator : MarshalByRefObject, IHelloGenerator
{
public string GetHelloString(string name)
{
return String.Format("Hello, {0}", name);
}
}
}

(2)将修改后的HelloGenerator发布出去

在这一步中,我们创建了一个新的Console应用程序RemotingServer,并在其中注册了一个Channel,发布服务并进行监听。代码如下:

using System;
using System.Configuration;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
namespace IocInCSharp
{
public class Server
{
public static void Main()
{
int port = Convert.ToInt32(ConfigurationSettings.AppSettings["LocalServerPort"]);
try
{
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = port;
props["timeout"] = 2000;
HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(EnHelloGenerator),
"HelloGenerator.soap",
WellKnownObjectMode.Singleton);
Console.WriteLine("Server started!\r\nPress ENTER key to stop the server...");
Console.ReadLine();
}
catch
{
Console.WriteLine("Server Start Error!");
}
}
}
}

(3)全新的客户端调用代码

为了使得客户端MainApp能够调用到远程的对象,我们需要修改它的代码,注册相应的Channel并进行远程访问。修改后的MainApp代码如下:

using System;
using System.Configuration;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
namespace IocInCSharp
{
public class MainApp
{
public static void Main()
{
//建立连接
BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = System.Convert.ToInt32(ConfigurationSettings.AppSettings["ClientPort"]);
HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(channel );
//创建远程对象
ISayHello sayHello = new SayHello();
string RemoteServerUrl = ConfigurationSettings.AppSettings["RemoteServerUrl"];
sayHello.HelloGenerator = (IHelloGenerator)Activator.GetObject(typeof(IHelloGenerator), RemoteServerUrl);
sayHello.SayHelloTo("zhenyulu");
}
}
}

在这段代码中,远程对象的创建是通过(IHelloGenerator)Activator.GetObject(typeof(IHelloGenerator), RemoteServerUrl)实现的。到此为止,我们就完成了对原有系统的Remoting改造。

经过调整后的系统,其组件间相互依赖关系如下图所示:

Pic011.gif

注意ICommon.dll文件在Client和Server端都有。

在整个调整过程中,我们修改了Server端的EnHelloGenerator以及CnHelloGenerator的代码,Client端的MainApp也 作了修改,以加入了远程访问机制。那么能不能对原有代码不作任何修改就实现远程访问机制呢?当然可以!不过我们还要请出Sping.net帮助我们实现这一切。

转载于:https://www.cnblogs.com/han-xiao-feng/archive/2006/07/25/459708.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值