淡水求咸

博客园已经停止更新,请移步 http://lovecjh.com/

导航

.NET Remoting(一)

今天实习中用到.NET Remoting,总结下:

需要建立四个工程:RemotingObject    RemotingServerObject     RemotingServer   RemotingClient

1.RemotingObject:

IOperation.cs

namespace RemotingObject
{
public interface IOperation
{
int Add(int a, int b);
int Subtract(int a,int b);
}
}

 

2.RemotingServerObject 

Operation.cs

using System;
using RemotingObject;

namespace RemotingServerObject
{
public class Operation:MarshalByRefObject,IOperation
{
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
}
}

 

3.RemotingServer 

TestClass.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingServer
{
class TestClass
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel(8888);
ChannelServices.RegisterChannel(tcpChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServerObject.Operation), "Operation.soap", WellKnownObjectMode.SingleCall);

// the server will keep running until key press.
Console.ReadKey();
}

}
}


4.RemotingClient

TestClass.cs

using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingObject;

namespace RemotingClient
{
class TestClass
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel,false);

IOperation operation = (IOperation)Activator.GetObject(typeof(IOperation), "tcp://localhost:8888/Operation.soap");
int result_Add = operation.Add(1,2);
int result_Subtract = operation.Subtract(4, 3);
Console.WriteLine(result_Add);
Console.WriteLine(result_Subtract);
}
}
}


 先运行RemotingServer,再运行RemotingClient

运行结果:

3
1
请按任意键继续. . .

 

理论基础:

(1).NET Remoting 基础

一、先阐述下什么是Remoting

在介绍Remoting的经典书《Advanced .NET Remoting》中,写到“Remoting is the process of programs or components interacting across certain boundaries”,Remoting是一种分布式处理方式,在.NET Framework下,Remoting 就是 DCOM 的一种升级,甚至可以说it simply replaces DCOM.Remoting 提供了跨进程的通信。

二、Remoting中有三个主体:远程对象(Remoting Object)   服务端(Server) 客户端(Client)

远程对象(Remoting Object)

远程对象一般分为两个部分实现,一个是提供公共接口,另一个是具体实现接口。一般服务端与客户端均引入公共接口,而具体实现接口只在服务端引入。

 

 服务端(Server)

在这里一般进行三步:

1.注册通道

TcpChannel tcpChannel = new TcpChannel(8888);
ChannelServices.RegisterChannel(tcpChannel, false);


其中8888为端口

2.注册远程对象

 

RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServerObject.Operation), "Operation.soap", WellKnownObjectMode.SingleCall);


其中RemotingServerObject.Operation是要注册的对象,Operation.soap是服务端uri WellKnownObjectMode.SingleCall是服务端激活模式,服务端激活模式有两种,一种是WellKnownObjectMode.SingleCall,另一种是WellKnownObjectMode.SingleTon。这两种激活模式的区别:SingleCall模式时,Remoting为每一个客户端建立一个远程实例,也就是当运行多个客户端时,彼此是独立的;SingleTon模式时,则 Remoting 将为所有客户端建立同个对象实例,也就是当时运行多个客户端时,这次运行的客户端用的对象实例都是第一次运行时的。

3.注销通道

如果要关闭 Remoting 的服务, 则需要注销通道,否则如果你想再一次注册该通道,会抛出异常。


客户端(Client)

在这里一般进行两步:

1.注册通道

TcpChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel,false);


2.获得远程对象

IOperation operation = (IOperation)Activator.GetObject(typeof(IOperation),    "tcp://localhost:8888/Operation.soap");

其中tcp://localhost:8888/Operation.soap是服务器端的uri,必须是上面的uri一致

posted on 2011-12-19 22:11  深圳彦祖  阅读(507)  评论(0编辑  收藏  举报