服务端层
在例子中,服务端层是由Server工程来实现。里面有一个windows窗体,它用来创建要加载的通道。内部的From1_Load事件的代码如下列表B。
列表B:
//Instantiate our server channel.
channel = newIpcServerChannel("ServerChannel");
//Register the server channel.
ChannelServices.RegisterChannel(channel, true);
//Register this service type.
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ServerMethods),
"ServerMethods",
WellKnownObjectMode.Singleton);
//Set the server's status to Good.
ServerData.Status = "Good";
//Set the server's start time to now.
ServerData.StartTime = DateTime.Now;
这段代码实例化一个IpcServerChannel类并命名为“ServerChannel”,再用ChannelService类来注册通道和注册服务,接着设置ServerData类的一些属性.
RegisterWellKnownServiceType调用非常重要,因为它告诉远端库它所能提供的对象和这些对象的URI。在上面代码的调用中,我们提供了一个ServerMethods对象。这个对象完整的URI近似于下面的地址,实际上这也是客户端用来连接服务端的URI:
ipc://ServerChannel/ServerMethods
URI的格式是:ipc://[channel_name]/[method_name]. “ServerChannel”的通道名用以下代码来定义:
//Instantiate our server channel.channel = newIpcServerChannel("ServerChannel");
客户端层
在例子中,客户端层是由Client工程来实现的,近似于Server工程,你能在客户端工程中找到一个独立的windows窗体,创建客户端通道并为IPC注册了一些类型。内部的Form1_Load事件的代码如下列表C。
列表C:
//Create an IPC client channel.
IpcClientChannel channel = newIpcClientChannel();
//Register the channel with ChannelServices.
ChannelServices.RegisterChannel(channel, true);
//Register the client type.
RemotingConfiguration.RegisterWellKnownClientType(
typeof(ServerMethods),
"ipc://ServerChannel/ServerMethods");
//Instantiate a new ServerMethods object. This is
// being instantiated in the current AppDomain, but
// since the ServerMethods object inherits from
// MarshalByRefObject, a proxy is created and calls
// on the ServerMethods object are passed to the
// server.
server = newServerMethods();
这段客户端的代码非常类似于服务端,除了客户端实例化IpcClientChannel而服务端实例化IpcServerChannel以外,还有就是用客户端用RegisterWellKnownClientType替代服务端里的RegisterWellKnownServiceType。RegisterWellKnownClientType的作用就是告诉远端基础设施客户端想要远程访问的类型(类)和通过什么URI来访问。
在我们这里的客户端例子中,我们通过URI ipc://ServerChannel/ServerMethods来远程访问ServerMethods类。请注意这里的URI和你在Server工程中建立的URI是一样的。
一旦通道和类型建立后,你就可以实例化这些类,其方式和你实例化其他C#类是完全一样的。
server = newServerMethods();
当以上声明执行后,远端基础设施会连接上服务端,创建一个新的对象并通过远程调用这个对象作为服务端到客户端的代理。btnCallServer_Click方法示范了这个过程,代码如下列表D。
列表D:
txtServerData.Clear();
txtServerData.Text = "Status: "+server.Status+" ";
txtServerData.Text += "Start: "+server.StartTime.ToString()+" ";
txtServerData.Text += "Is Processing: "+server.IsProcessing+" ";
每一个对“server”(实际上是代理)对象的调用都传到了IPC服务端,服务端再通过“Server”对象将数据返回给客户端。服务端可以在它的程序域中修改数据,并允许客户端从客户端的程序域中查看它的数据。
ServerMethods.IsProcessing属性就是一个好的例子。这个属性的值每2秒就在IPC服务端更新一次,从“Yes”到“No”,或者反过来。因为这个值客户端是可以访问到的,所以即使它不能改变这个值但也能看到值的变化。
运行例子
运行例子,先在Visual Studio 2005中打开InterprocessCommunication solution文件,然后点右键重建方案,方案重建后,打开Client和Server的创建目录。你先得双击“Server.exe”来执行服务端,然后双击“Client.exe”执行客户端。
客户端启动后,点“Call Server”按钮,你能在按钮下得文本框里看到以下的结果:
Status: Good
Start: 11/16/2006 3:17:19 PM
Is Processing: Yes
等大约一秒再点击“Call Server”按钮,点击后一会儿你就会看见“Is Processing”状态变为“No”。你也可以点击服务端程序的“Set Status to Bad”按钮,然后再点击客户端的“Call Server”按钮,你就可以看见“Status”变为“Bad”。
结束语
如你所见,IPC通道非常适用于从一个应用程序中监控另外一个应用程序的情况。特别是可用于那种没有提供用户接口但又要提供windows服务的服务端程序。假如你发现本例的下载和运行有问题,或你有任何别的相关问题,请随时与我联系。
责任编辑:德东