在 Remoting 中使用 Event 主要是为了实现 CallBack 机制,让服务器在接收到某个 "消息" 时,主动调用某个或多个客户端的方法。
在 Remoting 中使用 Event 主要是为了实现 CallBack 机制,让服务器在接收到某个 "消息" 时,主动调用某个或多个客户端的方法。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们先看一个例子。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Learn.Library.Remoting
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 委托类型
/// </summary>
public delegate void TestHandler();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 远程类型
/// </summary>
public class Data : MarshalByRefObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public TestHandler OnTest;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Test()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Test
(AppDomain:{0})", AppDomain.CurrentDomain.FriendlyName);
if (OnTest != null) OnTest();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class RemotingTest2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 服务器端代码
/// </summary>
static void Server()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AppDomain server = AppDomain.CreateDomain("server");
server.DoCallBack(delegate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BinaryServerFormatterSinkProvider bin = new BinaryServerFormatterSinkProvider();
bin.TypeFilterLevel = TypeFilterLevel.Full;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TcpServerChannel channel = new TcpServerChannel("server", 801, bin);
ChannelServices.RegisterChannel(channel, false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Data), "data",
WellKnownObjectMode.Singleton);
});
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 客户端代码
/// </summary>
static void Client()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownClientType(typeof(Data), "tcp://localhost:801/data");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Data data = new Data();
data.OnTest += delegate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("OnTest
(AppDomain:{0})", AppDomain.CurrentDomain.FriendlyName);
};
data.Test();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Server();
Client();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
输出:
Test
(AppDomain:server)
OnTest
(AppDomain:server)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果表明客户端事件方法 OnTest 被顺利执行。只不过结果有点问题,OnTest 是在服务器程序域内执行,这显然和我们设想服务器去通知客户端有所出入。这种方式实质上是将客户端委托方法一起序列化为消息传递到服务器端,然后在服务器应用程序域被执行,因此客户端是无法接收到所谓 "回调消息" 的。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
要实现我们所需要的 Remoting Event,需要做如下步骤:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1. 采取所谓 Duplex 方式。也就是说在客户端和服务器同时启用 ServerChannel 和 ClientChannel,因此我们需要使用 HttpChannel 或 TcpChannel。
2. 客户端事件方法应该是一个继承自 MarshalByRefObject 类型的实例方法。因为服务器是通过创建客户端的 MBR SAO 对象来实现回调的。
3. 缺省情况下,Delegate 无法被序列化,因此我们需要将服务器的 Formatter.TypeFilterLevel 设置为 Full。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
修改后的代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Learn.Library.Remoting
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 委托类型
/// </summary>
public delegate void TestHandler();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 远程类型
/// </summary>
public class Data : MarshalByRefObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public TestHandler OnTest;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Test()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Test
(AppDomain:{0})", AppDomain.CurrentDomain.FriendlyName);
if (OnTest != null) OnTest();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 客户端远程类型
/// </summary>
public class ClientData : MarshalByRefObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public void OnTestMethod()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Test
(AppDomain:{0})", AppDomain.CurrentDomain.FriendlyName);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class RemotingTest2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 服务器端代码
/// </summary>
static void Server()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AppDomain server = AppDomain.CreateDomain("server");
server.DoCallBack(delegate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BinaryClientFormatterSinkProvider cbin = new BinaryClientFormatterSinkProvider();
BinaryServerFormatterSinkProvider sbin = new BinaryServerFormatterSinkProvider();
sbin.TypeFilterLevel = TypeFilterLevel.Full;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Hashtable properties = new Hashtable();
properties["port"] = 801;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TcpChannel channel = new TcpChannel(properties, cbin, sbin);
ChannelServices.RegisterChannel(channel, false);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Data), "data",
WellKnownObjectMode.Singleton);
});
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 客户端代码
/// </summary>
static void Client()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TcpChannel channel = new TcpChannel(802);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownClientType(typeof(Data), "tcp://localhost:801/data");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Data data = new Data();
data.OnTest += new ClientData().OnTestMethod;
data.Test();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Server();
Client();
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted on
2007-06-05 09:10
编程山人
阅读(
315)
评论()
编辑
收藏
举报