.net remoting概念很多,先做个事例,然后对着事例慢慢讲解吧.
第一步:创建一个知名的远程对象(此类必须要继承MarshalByRefObject,至于为什么要继承,下面的内容再讲解)代码如下
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class DemoRemoting:MarshalByRefObject
{
public DemoRemoting()
{
Console.WriteLine("Demoting Start");
}
~DemoRemoting()
{
Console.WriteLine("Demoting abandon");
}
public void Shu()
{
Console.WriteLine("Welcom To My Blogs");
}
}
}
用csc /t:library DemoRemoting.cs命令编译为一个DemoRemoting.dll.
第二步:创建服务器端控制台程序演示.代码如下
Code
using System;
using System.Collections.Generic;
using System.Text;
using ClassLibrary1; //自己编译的dll命名空间哦
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp; //因为只用tcp通道,所以只引用了Tcp.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel tcp = new TcpServerChannel(8086);//注册信道,创建防火墙允许通过的端口
//信息的网友可以发现此构造函数数目蛮多的,其中有些重载的构造函数蛮实用的,下面再讲解
ChannelServices.RegisterChannel(tcp, false);//重载方法,关于后面false参数,下面内容在讲解
//下面注册的一些参数婷婷会在下面讲解
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DemoRemoting), "HI", WellKnownObjectMode.SingleCall);
Console.WriteLine("Waiting .");
Console.ReadLine();
}
}
}
启动后等待客户端连接.
第三步:编写客户端测试程序(为简单方便,所以还是使用控制台程序),代码如下
Code
using System;
using System.Collections.Generic;
using System.Text;
using ClassLibrary1; //自己编译的dll命名空间哦
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp; //因为只用tcp通道,所以只引用了Tcp.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpClientChannel tcp = new TcpClientChannel();
ChannelServices.RegisterChannel(tcp, false);
DemoRemoting demo = (DemoRemoting)Activator.GetObject(typeof(DemoRemoting), "tcp://localhost:8086/HI");
for (int i = 0; i < 5; i++)
{
Console.WriteLine(demo.Shu("C#"));
}
Console.Read();
}
}
}
现在也启动客户端程序,会发现如下图所示的图片.那表示你已经成功的做了个分布式程序了.
现在因该对remoting有初步的了解了吧.那我就继续往下讲理论知识吧.
remoting需要服务器端和客户端时时连接,这和MSMQ机制不一样.如果硬要拿这两种技术比较的话.我觉得就像datareader和dataset的区别.
remoting需要两端时时连接,如一方不在,那就会出现异常.而MSMQ就好象dataset一样,可以支持断开式,同步,异步的连接.
现在说说remoting的通信原理.
服务器端和客户端通过通道相互传送消息,下面先讲通道吧(学名是信道).
.net framework自带了3种类型的服务器端通道,分别是TcpServerChannel,HttpServerChannel,IpcServerChannel.其实我觉得我这么说是错的,因为这三种通道不但可以创建在服务器端,他们还可以创建在客户器端.因为他们继承了IChannelSender和IChannelReceiver.
IChannelSender和IChannelReceiver都是IChannel的派生类,IChannel是所有通道必须继承的基类(当我们创建定制的通道时).
IChannel有两个只读属性.分别是Channelname,ChannelPriority.分别是通道名称和级别.这两种属性在实例化一个通道时已经赋予了值了,因为它们只有get,没set,所以我们没办法改变它们默认值(这句话不是完全正确,虽然没办法通过set赋值,但我们可以通过它们的构造函数重新赋值).
tcp,http的name分别为tcp server,http server,它们的通信级别默认都是1,而ipc默认级别是20.下面我们通过构造函数来重赋值,代码如下.
Code
Dictionary<string, string> my = new Dictionary<string, string>();
my["name"] = "HTTP Server Channel";
my["priority"] = "15";
my["port"] = "8085";
BinaryServerFormatterSinkProvider sink = new BinaryServerFormatterSinkProvider();
HttpServerChannel hh = new HttpServerChannel(my, sink);
我相信细心的网友在实例化通道类时会发现它的构造函数多达5个.而其中一个是带idictionary和iserverchannelsinkprovider......
本想今天一次性把remoting写完,可发现刚写了信道就已经很累了,下次继续写吧.未完哦